You can show or hide something based on a user’s role with the current_user_can()
function. For the Administrator role you would check if current_user_can( 'administrator' )
.
Here’s a quick and dirty example of hiding a nav menu item by first checking if the title matches a particular page, and then checking if the current user is not an administrator, via the wp_nav_menu_objects
filter. If both conditions are satisfied, the menu item is removed via php’s unset
.
function wpa_filter_nav_menu_objects( $items ){
foreach( $items as $key => $item ){
if( 'Private page' == $item->title && !current_user_can( 'administrator' ) ){
unset( $items[$key] );
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'wpa_filter_nav_menu_objects' );
Related Posts:
- Hide pages depending on role
- Ad custom admin page like WordPress customizer [closed]
- add_menu_page does not render the expected result
- Changing the Order of Admin Menu Sections?
- Adding an Arbitrary Link to the Admin Menu?
- wp_nav_menu: show menu only if one exists, otherwise show nothing
- Add separator to admin submenu
- Add child pages automatically to nav menu
- Renaming a WordPress Admin Menu
- Automatically remove trashed pages from nav menu
- How do I fix this error: Warning: invalid argument supplied for foreach()?
- Directing a page to a default subpage
- How to add menu to Dashboard that can be viewed by all users
- Can you have different menus and theme on a per-page basis?
- List children of second level sub page
- Make Theme automatically choose default nav menu
- Move admin menu at the end
- How to make pages metabox display all hierarchy in menu admin?
- How to get current page menu item name instead of full menu item list
- Get parent page/menu id of current post
- How to display custom post meta in the appearance->menus page
- Change menu items URL
- How can I get an array/list of all current WordPress Admin Menu items?
- how to only add a top-level admin menu without it creating a sub-level-menu
- Making WordPress’ page and menus visible only for logged in users, checking session variables
- Different side menu on each page
- WordPress – site with 2 stylesheets and more…
- Remove Page Name From WordPress Menu
- Hiding Menu From Specific Pages
- Getting post content within wp_nav_walker
- wp_list_pages sort order is different for different languages [closed]
- Remove Metabox from Menus screen
- Sort wp_nav_menu() by menu order
- list pages only from master parent
- Custom Admin Menu Order
- wp_list_pages: only show subpages on the parent page?
- Disabling ‘posts’ in Appearance > Menus
- Hiding Admin Page While Keeping Menu Expanded
- Auto add pages to primary menu with functions.php
- Does loading of sub pages in menu cause load to the server?
- Get admin menu link
- WordPress menu permalink redirects to wrong page
- Hook for number of items in Appearance > Menus > CustomPostType > View All [duplicate]
- Link to Homepage in Menu
- Filter for Admin Nav Menus Drop Down
- Schedule Page to Menu [duplicate]
- Add class active to static menu
- SlideDeck 2, make back-end visible for admins only
- Limit the menu child levels under “admin > appearance > menus”
- wp_list_pages() but only show children on the branch you are on
- Check if Page Already Exists in Menu
- Programmatically add existing pages and subpages to Appearance Menu
- How to retrieve parent menu item name of current submenu item
- two different separate sections on the same site
- When clicking on menu item not going to specific page
- Admin menu structure
- Appearance Menus Screen – menu item drop down menu not functioning
- Adding an admin menu page showing the link twice – how to solve this?
- Displaying different page links on different pages
- Create custom page and add it in nav menu by code
- Does is_child() exist in wp 3.5.1?
- wp_list_pages sort order by custom nav menu order
- How to highlight 1st level menu item based on actual page
- Admin-ajax.php 500 error when Add to menu is clicked in admin
- How can I switch pages?
- Prevent menu from loading in a page template [duplicate]
- Can I have custom post types in “Add menu items” on in the Admin > Appearance > Menu
- Add Labels to Admin Menu ( How To )
- How to create a menu item which is not a page
- How to add a page to header that is outside of WordPress?
- How to Structure Pages and Subpages
- Highlighting top level link when viewing one of it’s sub-pages, which are not linked in the menu
- Three menus – parent / child / sibling
- Adding top level items in the admin menu that link to a post
- Add (grand)child pages automatically to nav menu
- how avoid the link of the current post in the menu in the sidebar?
- Disable parent in wp_list_pages – WordPress
- Page appears after de registering from menu
- Get parents child pages
- How to get submenu of admin menu?
- Strange menu behaviour wordpress admin [duplicate]
- Adding CSS-class to WordPress Menu in backend
- Menu deletes all items and position – wordpress bug?
- Plugins & Setting Menu Items Removed From Admin Panel
- Dynamic menu wp_list_pages displaying only current branch including: current page, his siblings and its childs
- wp_nav_menu without menu or theme_location specified
- I can’t add more pages to menu – menu delete pages
- wp_list_pages Hierarchical Help
- Parent Page > Child Page Menu
- Making pages in a menu appear conditionally
- Add pages content to startpage through custom menu
- Admin Menu Hack (Left side in Dashboard)
- Lost admin “manage_link” capability WordPress. No “Links” menu item in admin menu
- Main menu navigation links and new pages
- Page header menu links change
- Catagorized hierarchy menu for PAGES!
- How to show pages in menu without link
- Add a menu metabox to post edit page
- Setting or plugin for showing submenus horizontally under the main menu, and keep them after selection
- Page dependent sub-menu?