0

I'm just walking around in circles here trying to figure this out, but I have a custom user role Dealer who should only be able to edit a Page that he is the Author of. But I don't want this user to be able to create any new Pages.

I'm using the Member plugin to manage Roles currently.

I'd also like to ensure that the "Add New" menu item is removed for this user role. I've tried the follow to no avail:

function cm_redirect_users_by_role() {

    if ( ! defined( 'DOING_AJAX' ) ) {

        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];

        if ( 'dealer' === $role_name ) {
            remove_menu_page('post-new.php?post_type=page');
            // wp_redirect( 'http://www.mywebsite.com/' );
        } // if

    } // if DOING_AJAX

} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );
0

I'm not sure you are using the most correct way to restrict so maybe someone else can chime in but this if statement is grabbing the first role in the array which may be dealer or maybe another role if ( 'dealer' === $role_name ) {

Try instead if ($current_user->has_cap ('dealer')) { the has_cap function on a WP user will check the users capabilities AND roles https://codex.wordpress.org/Class_Reference/WP_User

Or this function https://codex.wordpress.org/Function_Reference/current_user_can

if (current_user_can('dealer'))

Also make sure that you have the correct capitalization 'Dealer' vs 'dealer'

  • Thanks! Any thoughts on the remove_menu_page not working for "Add New"? – dcolumbus May 10 '17 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.