adding existing menu page on new customer user role

I have created new user role ‘Host’ . now I am trying to show admin menu item ‘ user’ in it . i have googled some code and tried this

$result = add_role(
        'read'         => true // Use false to explicitly deny

function Host_menu_links(){
    $user = wp_get_current_user();
    if( $user_role == 'Host' ) {
    remove_menu_page( 'edit.php?post_type=page' ); 
    remove_menu_page( 'edit.php?post_type=videos' );
    remove_menu_page( 'edit.php' );


add_action('admin_menu', 'Host_menu_links');

I am trying to give only read permission to it .but it is not assigning any menu page after login as User .
I dont want to use plug in . How can I modify code or look for any other function to modify ?

Edit : I have also tried to add add_menu_page()

    function add_admin_menu_links(){
        $user = wp_get_current_user();

        $user_roles = $user->roles;
    $user_role = array_shift($user_roles);

        if( $user_role == 'subscriber' ) {

            /* add_menu_page('Tax Info', 'Tax Info', 'vendor', 'tax-info', 'vendor_tax_info_page');  --option 1 
            add_menu_page('users', 'users', 'subscriber', 'users', 'users.php');
--option 2 
            add_menu_page( __( 'Custom Menu Title', 'textdomain' ),'custom menu','manage_options','users.php','');*/
--option 3 


I have tried 3 options . i tried to get users menu for subcriber role but on clicking user page error come

‘You dont have permission to access that Page ‘

How can i modify it ?

WordPress Development Asked by Neeraj Verma on November 21, 2021

1 Answers

One Answer

Okay, I give you an answer based on capabilities and not role. You'll find a way to hide it for other role if you want (you have the code in your question). add_menu_page requires some parameters, hook and callback to work :

// Here is your hook to add page to the menu
add_action('admin_menu', 'wpse_288671_add_menu_pages');

// This function will add a page to your admin and your admin menu
function wpse_288671_add_menu_pages() {
        __( 'User page title', 'textdomain' ),
        __( 'User','textdomain' ), // It's your menu title
        'read', // This is the capability required to see this, here I use read
        'user_admin_page', // Here is your page slug 
        'wpse_288671_user_admin_page_callback', // Here is your function name to show content
        '', // Here your can add your custom icon URL
        null, // Here your can add the position (int)

// This function is the callback used in wpse_288671_add_menu_pages, that's the content of your page
function wpse_288671_user_admin_page_callback() {
<h2>Hey bro ! Can your read this ?</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

This code will add a menu link "User", with "User page title" page on click for people who have capability "read" (Super Admin, Administrator, Editor, Author, Contributor, Subscriber and your Host). More about role and capabilities on the doc :

Answered by Elex on November 21, 2021

Add your own answers!

Related Questions

Gutenberg withInstanceId. When to use it?

2  Asked on July 25, 2020 by at-least-three-characters


Ask a Question

Get help from others!

© 2021 All rights reserved.