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 1 year, 10 months ago by Vladimir.
-
AuthorPosts
-
20/01/2023 at 15:11 #8140jaycanuckParticipant
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 #8141VladimirKeymasterI 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 #8142VladimirKeymasterIf 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 #8144jaycanuckParticipantHi 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 #8147VladimirKeymasterHi,
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 #8148jaycanuckParticipantHi 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 #8149VladimirKeymasterSend the link to a new code version to support [at-sign] role-editor.com
24/01/2023 at 20:44 #8156jaycanuckParticipantThanks Vladimir. I sent an email to you.
27/01/2023 at 05:07 #8158VladimirKeymasterIn 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();
-
AuthorPosts
- You must be logged in to reply to this topic.