TransWikia.com

adding existing menu page on new customer user role

WordPress Development Asked by Neeraj Verma on November 21, 2021

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' ) {

            /* 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 ?

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

// 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() {
     add_menu_page(
        __( '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>
<?php
}

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 : https://codex.wordpress.org/Roles_and_Capabilities

Answered by Elex on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP