questions

wordpress show user for certain admin

Rate this post

My question is: How to edit/view some users profile to a specific administrator?

for example:

admin1 –> can edit and view specific user

admin2 –> can edit and view another specific user

is there any module for this?

 

✔️Solution:

You could check against the user id and the current page.

I’m using global $pagenow; because get_current_screen() don’t return any object on the users pages.

We define an array of restricted pages and allowed users IDs. All of that is hooked with admin_init which will only fire on an admin page. If one of our conditions fails then we display an error.

<?php
add_action( 'admin_init', function() {
    global $pagenow;
    $restricted_pages = [ //... array of restricted pages
        'users.php',
        'user-new.php',
        'profile.php',
    ];
    $allowed_users = [ //... array of allowed IDs
        1, //... 1 is the default super-admin ID
    ];
    if ( in_array( $pagenow, $base, true ) && ! in_array( get_current_user_id(), $allowed, true ) )
    wp_die( 'Hold your horses ? cowboy ! ' . esc_html( wp_get_current_user()->user_email ) . ' is not allowed in here !', 'Something went wrong...', [
        'back_link' => true,
    ] );
} );
?>

This goes in your function.php file.

Leave a Reply

Your email address will not be published.

Back to top button