1

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.

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
    Feb 20 at 15:39

1 Answer 1

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/

1
  • Thanks, I'm good with the auth side of it, my question is more one about what the best practice is in a wp plugin for adding a 'non-dashboard/admin section' page
    – Michael
    Feb 20 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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