restrict admin panel sections to users

admin2025-06-05  2

I have found out how to disable wordpress admin menu items from users other than administratos. What I would like to achieve now is every user can access only his user page (only "Your profile") and edit only some of the details, for example I don't want the user to change his email but he can change his nickname etc. Is that possible?

I have found out how to disable wordpress admin menu items from users other than administratos. What I would like to achieve now is every user can access only his user page (only "Your profile") and edit only some of the details, for example I don't want the user to change his email but he can change his nickname etc. Is that possible?

Share Improve this question asked Dec 3, 2018 at 8:31 dvn22dvn22 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Add the following, to functions.php of the current theme (child theme is preferred!):

CSS solution:

function no_email_changes_in_profile() {
    $screen = get_current_screen();
    if ('profile' == $screen->base && !current_user_can('manage_options')) {
        echo '<style>
input#email {
    pointer-events: none;
}
</style>';
    }
}
add_action('admin_head', 'no_email_changes_in_profile');

jQuery solution:

function no_email_changes_in_profile() {
    $screen = get_current_screen();
    if ('profile' == $screen->base && !current_user_can('manage_options')) {
        echo "<script>
jQuery(document).ready(function($) {
    $('#email').prop('readonly', true);
});
</script>";
    }
}
add_action('admin_head', 'no_email_changes_in_profile');

This will disable editing of 'Email' field on the profile admin page, for all users, except of an administrator.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749080738a316194.html

最新回复(0)