user roles - How to allow suscriber to access specific pages in dashboard?

admin2025-06-04  1

I want to create some pages in admin dashboard which are also shown to subscribers. Normally subscriber can see dashboard page and profile only. I want to create a page called Orders and show to subscribers. But not other pages.

How can I do ?

Thanks in Advance.

I want to create some pages in admin dashboard which are also shown to subscribers. Normally subscriber can see dashboard page and profile only. I want to create a page called Orders and show to subscribers. But not other pages.

How can I do ?

Thanks in Advance.

Share Improve this question asked Jan 26, 2019 at 13:18 Pradeep SapkotaPradeep Sapkota 1134 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

First off, you can add a menu (or an admin) page using:

  • add_menu_page() to add a top-level menu page

  • add_submenu_page() to add a submenu page

And there are also helper/wrapper functions you can use for adding a submenu page to the standard WordPress menu pages such as add_users_page() for the Users/Profile page. You can check the other available helper functions on the Related → Used By section on this page.

All those functions have a $capability parameter which is the minimum user/role capability required to view/access the menu page (and link). So you can use that parameter to restrict the menu page to certain users.

Here's an example for your case, where the role is subscriber which has the capability read:

function my_add_orders_menu_page() {
    add_menu_page(
        'Orders',             // Page title.
        'Orders',             // Menu title.
        'read',               // Capability.
        'my-orders',          // Menu slug.
        'my_orders_menu_page' // The callback that renders the menu page.
    );
}
add_action( 'admin_menu', 'my_add_orders_menu_page' );

function my_orders_menu_page() {
    echo 'Yay, it works!';
}

Note: You should read the note about option_page_capability_{$option_group} on this page.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748972811a315284.html

最新回复(0)