admin管理员组

文章数量:1122832

I am trying to remove some of the dashboard sections from the left menu for everyone but one user.

I have coded it like this:

//Removes Meta Boxes from Posts Edit Page
function rt_remove_meta_boxes() {
  remove_meta_box( 'postexcerpt', 'post', 'normal' );
  remove_meta_box( 'trackbacksdiv', 'post', 'normal' );
  remove_meta_box( 'postcustom', 'post', 'normal' );
  remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
  remove_meta_box( 'commentsdiv', 'post', 'normal' );
  remove_meta_box( 'revisionsdiv', 'post', 'normal' );
  remove_meta_box( 'authordiv', 'post', 'normal' );
  remove_meta_box( 'sqpt-meta-tags', 'post', 'normal' );
}

//Removes Custom Meta Boxes from Posts Edit Page
function rt_remove_custom_meta_boxes() {
  remove_meta_box( 'post_pagi_opt', 'post', 'normal' );
  remove_meta_box( 'advanced-sortables', 'post', 'normal' );
  remove_meta_box( 'pageparentdiv', 'post', 'normal' );
}

//remove items from menu in the left menu of the sitebackend (dashboard)
function rt_remove_menus(){
  remove_menu_page( 'edit.php?post_type=acf-field-group' );         // ACF
  remove_menu_page( 'jetpack' );                            // Jetpack
  remove_menu_page( 'edit-comments.php' );                      // Comments
  remove_menu_page( 'themes.php' );                             // Appearance
  remove_menu_page( 'plugins.php' );                            // Plugins
  remove_menu_page( 'tools.php' );                              // Tools
  remove_menu_page( 'options-general.php' );                    // Settings
  remove_menu_page( 'edit.php?post_type=acf' );                 // Custom Fields
  remove_menu_page( 'kadence-blocks' );                        // Kadence Setttings
  remove_menu_page( 'kadence-shop-kit-settings' );          // Kadence Setttings
}
if ( get_current_user_id() != 1) {
  add_action( 'admin_menu', 'rt_remove_menus' , 71);
  add_action( 'do_meta_boxes', 'rt_remove_custom_meta_boxes' );
  add_action( 'admin_menu', 'rt_remove_meta_boxes' );
}

The problem is with the get_current_user.

My user id is 1 when i look at the wp-json list of users. When I look in the backend my user doesn't have a user id in the slug:

My user: /wp-admin/profile.php?wp_http_referer=%2Fwp-admin%2Fusers.php

Other user example: /wp-admin/user-edit.php?user_id=3&wp_http_referer=%2Fwp-admin%2Fusers.php

All the same when I use the code it doesn't hide the content.

I've tried: if ( get_current_user_id() == 1) with no change if ( get_current_user_id() != 0) it hides for everyone

I was hoping not to create a new role as this is only for my user. Is it possible?

I tried adding my last if statement into a function and setting to admin but I'm still not seeing my hopeful changes:

function rt_user_test(){
  if ( get_current_user_id() == 1) {
    add_action( 'admin_menu', 'rt_remove_menus' , 71);
    add_action( 'do_meta_boxes', 'rt_remove_custom_meta_boxes' );
    add_action( 'admin_menu', 'rt_remove_meta_boxes' );
  }
}

add_action( 'admin_init', 'rt_user_test' );

I am trying to remove some of the dashboard sections from the left menu for everyone but one user.

I have coded it like this:

//Removes Meta Boxes from Posts Edit Page
function rt_remove_meta_boxes() {
  remove_meta_box( 'postexcerpt', 'post', 'normal' );
  remove_meta_box( 'trackbacksdiv', 'post', 'normal' );
  remove_meta_box( 'postcustom', 'post', 'normal' );
  remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
  remove_meta_box( 'commentsdiv', 'post', 'normal' );
  remove_meta_box( 'revisionsdiv', 'post', 'normal' );
  remove_meta_box( 'authordiv', 'post', 'normal' );
  remove_meta_box( 'sqpt-meta-tags', 'post', 'normal' );
}

//Removes Custom Meta Boxes from Posts Edit Page
function rt_remove_custom_meta_boxes() {
  remove_meta_box( 'post_pagi_opt', 'post', 'normal' );
  remove_meta_box( 'advanced-sortables', 'post', 'normal' );
  remove_meta_box( 'pageparentdiv', 'post', 'normal' );
}

//remove items from menu in the left menu of the sitebackend (dashboard)
function rt_remove_menus(){
  remove_menu_page( 'edit.php?post_type=acf-field-group' );         // ACF
  remove_menu_page( 'jetpack' );                            // Jetpack
  remove_menu_page( 'edit-comments.php' );                      // Comments
  remove_menu_page( 'themes.php' );                             // Appearance
  remove_menu_page( 'plugins.php' );                            // Plugins
  remove_menu_page( 'tools.php' );                              // Tools
  remove_menu_page( 'options-general.php' );                    // Settings
  remove_menu_page( 'edit.php?post_type=acf' );                 // Custom Fields
  remove_menu_page( 'kadence-blocks' );                        // Kadence Setttings
  remove_menu_page( 'kadence-shop-kit-settings' );          // Kadence Setttings
}
if ( get_current_user_id() != 1) {
  add_action( 'admin_menu', 'rt_remove_menus' , 71);
  add_action( 'do_meta_boxes', 'rt_remove_custom_meta_boxes' );
  add_action( 'admin_menu', 'rt_remove_meta_boxes' );
}

The problem is with the get_current_user.

My user id is 1 when i look at the wp-json list of users. When I look in the backend my user doesn't have a user id in the slug:

My user: /wp-admin/profile.php?wp_http_referer=%2Fwp-admin%2Fusers.php

Other user example: /wp-admin/user-edit.php?user_id=3&wp_http_referer=%2Fwp-admin%2Fusers.php

All the same when I use the code it doesn't hide the content.

I've tried: if ( get_current_user_id() == 1) with no change if ( get_current_user_id() != 0) it hides for everyone

I was hoping not to create a new role as this is only for my user. Is it possible?

I tried adding my last if statement into a function and setting to admin but I'm still not seeing my hopeful changes:

function rt_user_test(){
  if ( get_current_user_id() == 1) {
    add_action( 'admin_menu', 'rt_remove_menus' , 71);
    add_action( 'do_meta_boxes', 'rt_remove_custom_meta_boxes' );
    add_action( 'admin_menu', 'rt_remove_meta_boxes' );
  }
}

add_action( 'admin_init', 'rt_user_test' );
Share Improve this question edited May 21, 2024 at 4:13 rudtek asked May 20, 2024 at 18:06 rudtekrudtek 6,3535 gold badges30 silver badges52 bronze badges 2
  • Have you tried to get this working for a user with another ID than 1? – cjbj Commented May 21, 2024 at 10:15
  • 1 Your code uses get_current_user_id() but your question mentions get_current_user(), which is a PHP function. The WordPress equivalent is wp_get_current_user(). I say this as someone who once spent an entire work day trying to track down that particular problem. – Pat J Commented May 21, 2024 at 19:15
Add a comment  | 

1 Answer 1

Reset to default 2

If you run get_current_user_id() too early, like you're doing, it will just return 0.

Wrap your logic containing that function inside a hook, like init, or inside the rt_* callbacks, to avoid that.

Note that init fires right after current user setup and before admin_menu (src)

本文标签: Add an action only for a specific user in a plugin