0

Initially I intended to fetch user ID so I could show/hide certain menu groups in my plugin based on user status (which I added as a meta).

The problem is that I can't use wp_get_current_user and get_current_user_id returns 0.

Is there any other way to get this ID? Or is there another way to hide/show menu based on logged in user status?

The structure almost looks like this.

add_action("admin_menu","show_menu");
function show_menu(){
    //create menu1;
    //create menu2;

    if () //check user status
        //create menu3
}
0

You mention user 'status'....are you looking to show/hide the menu for classes of users (by role/capability) rather than individual users (by ID)?

I do something similar so that users below a level cannot access certain back-end menu items, using current_user_can and !current_user_can.

As an example, this checks to see if a user is an Editor or higher, and if not does not allow the user to see ANY Posts except their own (default WP behavior is to show all Posts but only allow them to edit their own, we don't want them to even see others' Posts).

add_action( 'load-edit.php', 'posts_for_current_contributor' );
function posts_for_current_contributor() {
    global $user_ID;
    if ( !current_user_can( 'delete_others_posts' ) ) {
       if ( ! isset( $_GET['author'] ) ) {
          wp_redirect( add_query_arg( 'author', $user_ID ) );
          exit;
       }
   }

}

You can learn more about Roles & Capabilities with some other examples here: https://codex.wordpress.org/Roles_and_Capabilities

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.