Skip to content Skip to main menu
User Role Editor
Menu
  • Home
  • Prices
  • Download
  • Testimonials
  • Forums
  • Blog
  • ChangeLog
  • Documentation
  • Login
Home › Topic › User Profile Fields

Change WordPress user roles and capabilities › Forums › Give user access to plugin – how to › User Profile Fields

Tagged: php, user profile

  • This topic has 8 replies, 2 voices, and was last updated 2 years, 3 months ago by Vladimir.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • 20/01/2023 at 15:11 #8140
    jaycanuck
    Participant

    Hi. I followed your instructions on role-editor.com/hide-disable-wordpress-user-profile-fields/ to disable elements of an author’s user profile (authors don’t need everything that’s there)

    I unfortunately get a “syntax error, unexpected ‘global’ (T_GLOBAL)” error when saving my functions.php

    This is the code I’m putting in:

    function user_profile_fields_disable() {
     
    global $pagenow;
     
    // apply only to user profile or user edit pages
    if ($pagenow!==’profile.php’ && $pagenow!==’user-edit.php’) {
    return;
    }
     
    // do not change anything for the administrator
    if (current_user_can(‘administrator’)) {
    return;
    }
    if (current_user_can(‘custom_role’) {
    add_action( ‘admin_footer’, ‘user_profile_fields_disable_js’ );
    }
    }

    /**
    * Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
    */
    function user_profile_fields_disable_js() {
    ?>
    <script>
    jQuery(document).ready( function($) {
    var fields_to_disable = [‘role’, ‘facebook’, ‘twitter’, ‘additional_profile_urls’, ‘wp-user-avatar-existing,];
    for(i=0; i<fields_to_disable.length; i++) {
    if ( $(‘#’+ fields_to_disable[i]).length ) {
    $(‘#’+ fields_to_disable[i]).attr(“disabled”, “disabled”);
    }
    }
    });
    </script>
    <?php
    }

    20/01/2023 at 16:03 #8141
    Vladimir
    Keymaster

    I see 2 possible problems in the code above:
    1) Look if single quote “‘” is not replaced at your copy with other characters;
    2) at the JavaScript code line
    var fields_to_disable = ['role', 'facebook', 'twitter', 'additional_profile_urls', 'wp-user-avatar-existing,];
    there is a syntax error. Replace the ‘,’ at the end with missed “‘” (closing single quote) character, like below:
    var fields_to_disable = ['role', 'facebook', 'twitter', 'additional_profile_urls', 'wp-user-avatar-existing'];

    20/01/2023 at 16:04 #8142
    Vladimir
    Keymaster

    If you will need further help, send or share via Google Drive a modified functions.php file. I will check its syntax.

    23/01/2023 at 18:14 #8144
    jaycanuck
    Participant

    Hi Vladimir. Thanks for helping me with this. Here is the link:

    [link removed]

    It’s now giving me this syntax error:

    Your PHP code changes were rolled back due to an error on line 234. Please fix and try saving again. This would be this line:

    234 – add_action( ‘admin_footer’, ‘user_profile_fields_disable_js’ );

    24/01/2023 at 04:52 #8147
    Vladimir
    Keymaster

    Hi,

    Syntax error is at the prev line – missed closing bracket ‘)’ after (‘custom_role’). It should be:

    
        if (current_user_can('custom_role')) {
          add_action( 'admin_footer', 'user_profile_fields_disable_js' );
        }      
    
    
    24/01/2023 at 11:50 #8148
    jaycanuck
    Participant

    Hi Vladimir. Thanks for this.

    So, this code follows the instructions to hide fields in the user profile area. When I enter in the field IDs though….they are still there in the author role.

    I uploaded the new code with your changes and some of the fields I’m trying to hide, to the Google Drive. Am I missing something?

    24/01/2023 at 12:55 #8149
    Vladimir
    Keymaster

    Send the link to a new code version to support [at-sign] role-editor.com

    24/01/2023 at 20:44 #8156
    jaycanuck
    Participant

    Thanks Vladimir. I sent an email to you.

    27/01/2023 at 05:07 #8158
    Vladimir
    Keymaster

    In the provided piece of code the key line is missed:

    
    add_action('admin_init', 'user_profile_fields_disable');
    

    It links your function to the hook which executes it.

    Also pay attention that JavaScript code disables only input fields. It can not disable any other visible element. If you wish to hide some element on a page you should use another jQuery code, like

    
    jQuery('#div_id').hide();
    
  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Log In

Recent Topics

  • Editor role – Siteground Speed Optmiser – Flush Cache
  • Editor Access to Appearance / Menus
  • WP Version 6.8 – Text Domain Error
  • Prevent Unauthenticated Access to CPT by Default
  • Other roles not restricted
  • Admin not having WP backend menu items
  • One admin has reduced capabilities
  • Download Monitor
  • Multisite – Capabilities reset when restricting access to a plugin
  • Attaching roles to groups

Recent Replies

  • VC Team on Admins can’t see new CPTs 1 day, 22 hours ago
  • Stephen001 on Editor Access to Appearance / Menus 1 week, 3 days ago
  • Vladimir on Editor Access to Appearance / Menus 1 week, 4 days ago
  • Vladimir on WP Version 6.8 – Text Domain Error 3 weeks, 6 days ago
  • Vladimir on WP Version 6.8 – Text Domain Error 4 weeks ago
  • camber799 on Prevent Unauthenticated Access to CPT by Default 1 month, 1 week ago
  • Channel Digital on Multisite – Capabilities reset when restricting access to a plugin 1 month, 3 weeks ago
  • Channel Digital on Multisite – Capabilities reset when restricting access to a plugin 2 months ago
  • [email protected] on Attaching roles to groups 2 months ago
  • riomastri on Display post based on user id 2 months ago
  • Contact Us
  • Privacy Policy
  • Terms of use (EULA)
© 2010 - 2025 Artellect Limited. Units A-C, 25/F., Seabright Plaza, No. 9-23 Shell Street, North Point, Hong Kong. Contact: hd@role-editor.com
Share