Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CedricL46/abcb41c81f0b186ca4e2b2875b3559bd to your computer and use it in GitHub Desktop.
Save CedricL46/abcb41c81f0b186ca4e2b2875b3559bd to your computer and use it in GitHub Desktop.
<?php
//Add the following in your custom plugin code
//Note: if you prefer to add it in the function.php remove the array : add_filter('wp_nav_menu_args', 'display_custom_menu');
add_filter('wp_nav_menu_args', array($this,'display_custom_menu'));
/**
* Filter menu to display
* logged-in menu if user is logged in
* logged-in-admin menu if user is logged in and is an Administrator
* logged-out menu if visitor isn't logged in
*
*/
public function display_custom_menu($args = '')
{
//check if user is logged in
if (is_user_logged_in()) {
//check if logged in user have admin privileges
if (current_user_can('manage_options')) {
$args['menu'] = 'logged-in-admin';
} else {
$args['menu'] = 'logged-in';
}
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
//Note : Now you just have to create 3 menus named logged-in-admin / logged-in / logged-out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment