Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created September 7, 2012 01:56
Show Gist options
  • Select an option

  • Save wpsmith/3662529 to your computer and use it in GitHub Desktop.

Select an option

Save wpsmith/3662529 to your computer and use it in GitHub Desktop.
Modifies the Navigation Menu based on current user capabilities
add_filter( 'wp_nav_menu_args', 'child_nav_menu_args' );
/**
* Modifies the Navigation Menu based on current user capabilities
*
* @uses gbootstrap_nav_args() Sets Genesis Bootstrap Navigation Args for wp_nav_menu().
* @param array $args Arguments for wp_nav_menu()
* @return array $args Modified Arguments for wp_nav_menu()
* @since 1.0.0
*/
function child_nav_menu_args( $args ) {
// Make sure we are on the Custom Menu Widget; change '' to whatever theme location you wish to modify.
// Custom Menu Widget does not have a location
// Assumes that all other uses of wp_nav_menu() are for properly registered locations.
if ( '' != $args['theme_location'] ) return;
// Change this to your menu ids. This can also take menu slugs, or even name.
$menus = array(
'administrator' => 3,
'editor' => 4,
'author' => 7,
'contributor' => 8,
'subscriber' => 9,
);
$current_user = wp_get_current_user();
// Highest cap first
if ( $current_user->has_cap( 'manage_options' ) )
$args['menu'] = $menus['administrator'];
if ( $current_user->has_cap( 'edit_others_posts' ) )
$args['menu'] = $menus['editor'];
if ( $current_user->has_cap( 'publish_posts' ) )
$args['menu'] = $menus['author'];
if ( $current_user->has_cap( 'edit_posts' ) )
$args['menu'] = $menus['contributor'];
if ( $current_user->has_cap( 'read' ) )
$args['menu'] = $menus['subscriber'];
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment