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( 'Host', __(Host), array( 'read' => true // Use false to explicitly deny ) ); function Host_menu_links(){$user = wp_get_current_user();
if( $user_role == 'Host' ) { remove_menu_page('tools.php'); remove_menu_page('themes.php'); remove_menu_page('options-general.php'); remove_menu_page('plugins.php'); remove_menu_page('edit-comments.php'); remove_menu_page('page.php'); remove_menu_page('upload.php'); 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' ) {

--option 2
--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

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 : https://developer.wordpress.org/reference/functions/add_menu_page/

// Here is your hook to add page to the menu

__( 'User page title', 'textdomain' ),
'read', // This is the capability required to see this, here I use read
);
}

?>
<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>
<?php
}


Answered by Elex on November 21, 2021