WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jul 27 2010

Using Custom Menus to Create Custom Menus Based on Role/Capabilities

So thanks to Nathan Rice (@nathanrice), creator of Genesis, I was able to complete my code to create menus based on user roles and capabilities. On one of my sites, the user wanted to display three different menus based on one's user roles and capabilities:

  1. One to display if a person was a Contributor or higher (read, edit posts, etc.).
  2. One to display if a person was a Subscribor (read)
  3. One to display for a visitor (none)

So as I stumbled through the code, here is the final code for Genesis Users. For non-Genesis users simply change 'menu_class' => genesis_get_option('nav_superfish') ? 'nav superfish' : 'nav', to 'menu_class' => '',. And you will need to change the add_action and remove_action code.

[php]//Custom Menu Based on Roles/Capabilities
remove_action('genesis_after_header', 'genesis_do_nav');
add_action('genesis_after_header', 'custom_genesis_do_nav');
function custom_genesis_do_nav() {

if ( current_user_can('edit_posts') ) {
$nav = wp_nav_menu(array(
'theme_location' => 'primary',
'menu' => 1,
'container' => '',
'menu_class' => genesis_get_option('nav_superfish') ? 'nav superfish' : 'nav',
'echo' => 0
));
printf( '<div id="nav"><div class="wrap">%s</div></div>', $nav );
}
elseif ( current_user_can('read') && !current_user_can('edit_posts') ) {
$nav = wp_nav_menu(array(
'theme_location' => 'primary',
'menu' => 2,
'container' => '',
'menu_class' => genesis_get_option('nav_superfish') ? 'nav superfish' : 'nav',
'echo' => 0
));
printf( '<div id="nav"><div class="wrap">%s</div></div>', $nav );
}
elseif(!current_user_can('read') && !current_user_can('edit_posts') ) {
$nav = wp_nav_menu(array(
'theme_location' => 'primary',
'menu' => 3,
'container' => '',
'menu_class' => genesis_get_option('nav_superfish') ? 'nav superfish' : 'nav',
'echo' => 0
));
printf( '<div id="nav"><div class="wrap">%s</div></div>', $nav );

}
else //just in case
{
genesis_do_nav();
}

} [/php]

To get the menu id number, simply go to Appearance > Menus. When you mouse over the menus, you should see a URL like http://mydomain.com/wp-admin/nav-menus.php?action=edit&menu=44. The menu=44 is your menu ID. When you click on them the URL should appear in the address bar if you cannot see the URLs in the status bar.

Written by Travis Smith · Categorized: Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Ujjwal Kumar says

    November 25, 2011 at 8:57 am

    I made this code do what I wanted after a little modification. Thanks Travis 😀

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in