admin管理员组

文章数量:1125931

I am building a plugin with two pages, one will be part of the admin dashboard which is simple enough and will just allow admins to edit some data. The other page should also be only accessible for admins and will allow them to view the data but as a 'normal' page, i.e. no wp sidebar menu or anything like that.

I had a look into the remove_menu_page function but even after I remove all admin pages there is still an empty menu bar at the left-hand side, it also defeats the object if it means admins can't access the page I'm building. There were some suggestions to just hide the menu with css but I'm not sure whether this would be best practice.

It's possible that I'm using the wrong paradigm to look at this and I should instead create a shortcode for users to add on a standard wordpress page that can then be locked down. That feels against the idea of it being an 'admin' page though.

I am building a plugin with two pages, one will be part of the admin dashboard which is simple enough and will just allow admins to edit some data. The other page should also be only accessible for admins and will allow them to view the data but as a 'normal' page, i.e. no wp sidebar menu or anything like that.

I had a look into the remove_menu_page function but even after I remove all admin pages there is still an empty menu bar at the left-hand side, it also defeats the object if it means admins can't access the page I'm building. There were some suggestions to just hide the menu with css but I'm not sure whether this would be best practice.

It's possible that I'm using the wrong paradigm to look at this and I should instead create a shortcode for users to add on a standard wordpress page that can then be locked down. That feels against the idea of it being an 'admin' page though.

Share Improve this question asked Feb 20, 2024 at 10:48 MichaelMichael 112 bronze badges 1
  • just add a "normal" page as you normally would but use the code in the answer to mark that page content so only admins can see it. Use the same function or a shortcode to call the data that you need. – rudtek Commented Feb 20, 2024 at 15:39
Add a comment  | 

1 Answer 1

Reset to default 1

You'll need something like this:

function your_callback() {

 if ( ! current_user_can( 'manage_options' ) ) {
    return;
 }

 // add your code here to display the menu for admins only

}

Basically you need to check what capabilities the current user has and then select one which only admins have

List of Roles & Capabilities https://wordpress.org/documentation/article/roles-and-capabilities/

本文标签: How can I have an admin only nondashboard page in my Wordpress plugin