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?
- Add separator to admin submenu
- 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
- How to get current page menu item name instead of full menu item list
- How to display custom post meta in the appearance->menus page
- How can I get an array/list of all current WordPress Admin Menu items?
- 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…
- 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
- 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
- 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?
- 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
- Prevent menu from loading in a page template [duplicate]
- Add Labels to Admin Menu ( How To )
- How to add a page to header that is outside of WordPress?
- Three menus – parent / child / sibling
- 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
- 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
- I can’t add more pages to menu – menu delete pages
- Parent Page > Child Page 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
- Page dependent sub-menu?
- Admin menu in different languages
- Add prefix to url, but products not showing in page SHOP
- adding pages in underscores.me theme
- How to get the Page Name of the current page?
- Need to remove the admin menu item from a be theme in wordpress
- Removing wp admin menu item on top bar of admin backend
- Add CSS Class to WordPress Menu Item Dependent on the Page the User is Visiting
- How can I order these pages in this navigation bar on this old wordpress theme?
- Unable to remove page from primary menu
- how to create pages and link them to a menu
- How to sort a sub-menu, generated with get_pages(), by the page order instead of alphabetically?
- wp_is_mobile() inside js
- How to add class and span to
- Specific Page that convert Hyperlink to anchor in nav menu
- Appearance > Menus
- WordPress – Apperance > Menu
- Creating and populating custom nav menu (menu-item-position not working)
- How do I fix the URL used in the WordPress admin menu for customize.php
- How to make a page to not appear in the menu when created?
- How can I disable sorting/dragging of a menu item in the custom menu admin?
- Site with all content on the front page
- Adding Pages to Menus – No Page Hierarchy in Backend [duplicate]
- How can I make this process automatic?
- How to change the markup created by menu item only on chosen items?
- How does WP knows which template to use for a page
- Problems with draft website pages
- Control page access based on User Meta data?
- How to change WP menu item type?