0

iHi,

I use this snippet in a plugin;

if ( ! ( current_user_can( 'manage_options' ) ) )
  do something
 }

but problem is, when network activating this on a multi-site installation, I get locked out of admin pages. WP says “You don’t have authority to this page” or something (translating from Swedish).

I have, by trial and error, limited down the problem to the function "current_user_can" but is_super_admin gives the same result. I get locked out. Please note that this only happens when network activating on a multisite installation. If I activate the plugin per site everything works as expected. See github for an example of how I try to implement it.

How come? I’ve tried it out many times and tried to tweak the code, but I can't get it to work.

Any better way of checking it user does NOT have authority to manage options (does not have admin capabilities)?

3
  • 1
    Perhaps it's related to if (!(current_user_can('manage_options'))) { being run immediately rather than being used within a callback function fired on a hook? Can you try wrapping this functionality in a function and firing it on wp_loaded? (wp_loaded seems early enough.) Sep 27 '16 at 5:07
  • I can't mark your comment as a solution. I will mark my sum up of your answer as a solution tomorrow. Sep 27 '16 at 12:43
  • Works for me! Glad I was able to help. Sep 27 '16 at 20:36
3

Thanks for your advice. In fact it is a hook that I fire of with this check. It looks like this (an example, the check is done in two places for different user levels and in two different plugins)

// Down locks me out of admin area if network activated...

if (!(current_user_can('manage_options'))) { add_action( 'admin_menu', 'ngo_remove_eo' ); }

and the function is;

function ngo_remove_eo() {
remove_menu_page( 'edit.php?post_type=event' );

}

I changed it to this;

   add_action( 'wp_loaded', 'eongo_cleanup' );

function eongo_cleanup() {
    if (!(current_user_can('manage_options'))) {  
    add_action( 'admin_menu', 'ngo_remove_eo' );
    }

    if(!is_super_admin()){
    add_action( 'admin_menu', 'ngo_remove_eosubmenu', 99 );
    add_action( 'do_meta_boxes', 'remove_eopost_custom_fields' );
    }
}

and now it works :) Thanks a lot Dave.

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.