How to hide an item from a menu to logged out users (without a plugin)

I want to hide an item from a menu if a user is logged out.

I am currently using the below code that achieves this using two separate menus, but to save duplication, I would like to only have to manage one nav menu.

function my_wp_nav_menu_args( $args = '' ) {      if ( is_user_logged_in() ) {          $args['menu'] = 'logged-in';     } else {          $args['menu'] = 'logged-out';     }      return $args; } add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' ); 

Is it possible to hide just one item for a logged out user, rather than doing it the way I currently am?


Filter wp_nav_menu_objects. It will contain the sorted list of nav menu items to render. Have a look at wp_setup_nav_menu_item for some properties you can use.

Here's a quick (untested) example.

add_filter('wp_nav_menu_objects', 'wpse233667_nav_menu_objects', 10, 2);
function wpse233667_nav_menu_objects($items, $args)
  if ('someThemeLocation' !== $args->theme_location) {
    return $items;

  $out = [];
  foreach ($items as $item) {
    if ('/user-specific-thingy' === $item->url && !is_user_logged_in()) {
    $out[] = $item;

  return $out;

Find the class or id of the menu item that you want to hide. suppose the class of that menu is logged-in-menu

Then in header.php file of your theme before closing head tag use the below code

if(! is_user_logged_in() ) : ?>
<?php endif; ?>

Category: menus Time: 2016-07-31 Views: 0
Tags: menus

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.198 (s). 12 q(s)