May 16, 2023

Hide WordPress Sidebar Items for Specific User Roles with Code Snippets

Play Video

What is the Tutorial About?

See how to hide specific items on the WordPress sidebar for different user roles using a free Code Snippet. This method, explained by Web Squadron, simplifies the process, eliminates the need for a child theme, and allows for easy rollback in case of any issues. By customizing the sidebar for user roles, you can improve the user experience and provide a more organized interface for each type of user.

Use Cases for this Tutorial

  • Simplify the WordPress dashboard for users with limited permissions, such as authors or contributors, by hiding unnecessary sidebar items
  • Streamline the interface for editors, allowing them to focus on content management by hiding settings, appearance, and other non-essential options
  • Enhance website security by restricting access to certain dashboard items based on user roles, ensuring that sensitive settings are only accessible to authorized users

Required Code

Source: https://www.codesnippets.cloud/snippet/WebSquadron/Hide-Wordpress-Items-based-on-User-Role


function remove_dashboard_menu_items() {


    global $menu;


    $user = wp_get_current_user();


    $user_role = $user->roles[0]; // Get the first user role assigned to the user


 


    if ($user_role == 'administrator') {


        // Admin can see all menu items


        return;


    } elseif ($user_role == 'editor') {


        // Editors can see Pages and Posts


        unset($menu[25]);   // Removes "Comments"


        unset($menu[60]);   // Removes "Appearance"


        unset($menu[65]);   // Removes "Plugins"


        unset($menu[70]);   // Removes "Users"


        unset($menu[75]);   // Removes "Tools"


        unset($menu[80]);   // Removes "Settings"


        remove_menu_page('index.php'); // Removes the Dashboard home page


    } else {


        // All other users see limited menu items


        unset($menu[5]);    // Removes "Posts"


        unset($menu[10]);   // Removes "Media"


        unset($menu[20]);   // Removes "Pages"


        unset($menu[25]);   // Removes "Comments"


        unset($menu[60]);   // Removes "Appearance"


        unset($menu[65]);   // Removes "Plugins"


        unset($menu[70]);   // Removes "Users"


        unset($menu[75]);   // Removes "Tools"


        unset($menu[80]);   // Removes "Settings"


        remove_menu_page('index.php'); // Removes the Dashboard home page


    }


}


add_action('admin_menu', 'remove_dashboard_menu_items');

Required Resources

Code Snippets Logo
Free Options
Credit to Web Squadron
Web Squadron is a popular YouTube channel dedicated to providing informative tutorials, tips, and insights on web development and design. With a strong focus on WordPress and website builders like Bricks Builder, the channel aims to help users of all skill levels, from beginners to advanced developers, create and optimize their websites. By offering easy-to-understand, step-by-step guidance, Web Squadron provides practical and valuable content.
Visit
Hide WordPress Sidebar Items for Specific User Roles with Code Snippets
Welcome back!
Enter your Helwp credentials to sign in.

No Account yet? Sign Up

My Account
Menu
Give Feedback
Describe your feedback *
Rate Helwp
Share
Facebook
Twitter
LinkedIn
Reddit
Email
WhatsApp
Telegram
Pocket
Report
Problem *
Describe the problem
Want us to reply?
Your E-Mail
Affiliate Disclosure

At Helwp, we’re committed to transparency and honesty. Therefore, we want to inform you that some of the links on our website are affiliate links. This means that, at no additional cost to you, we may earn a small commission if you click through and make a purchase.

We only promote products or services that we genuinely believe in. These affiliate commissions help us to maintain the website and continue to provide you with free, high-quality WordPress content.

If you are interested in how you can support us even further, check out our support page.