1

Scenario I have three pages namely John James and Jessica. The Jessica page ID is 666 and I want to filter it out so that it DOES NOT show in the menu. I need help with a simple code similar to the one pasted below, that will help me achieve that. I have deliberately not registered any menu location in my function.php file.

function remove_jessica_page(){
    #
    wp_nav_menu(
        array(
            'exclude'=> 666 // exclude Jessica page from  menu
        ) 
    );
}
apply_filters('wp_nav_menu_items','remove_thankyou_page');
3
  • 1
    normally this is done manually, but you mentioned you aren't registering nav menus deliberately, what's the context/reasoning behind this? I ask because some of the answers might not work for you.
    – Tom J Nowell
    Commented Jun 28 at 10:22
  • I intend to use the concept in plug-making, and Different themes that may use the plugin will have different registered nav menus and locations. Is my approach wrong? Commented Jul 3 at 13:47
  • hmm that's good to know, though if your plugin is creating the post and you know the slug it's using, then CSS could hide the menu item and you wouldn't need PHP to remove the HTML
    – Tom J Nowell
    Commented Jul 3 at 14:20

1 Answer 1

1

That's not the filter to use, instead use the wp_nav_menu_objects filter documented at: https://developer.wordpress.org/reference/hooks/wp_nav_menu_objects/

The docs contain a helpful code example provided by a contributor at the bottom, which I've modified slightly:

function wpse_unset_menu_items( $menu_objects, $args ) {
    // remove this if you want it on all menus not just a specific menu aka primary_menu:
    if ( 'primary_menu' !== $args->theme_location ) {
        return $menu_objects;
    }

    // if the user is logged in, don't do anything.
    if ( is_user_logged_in() ) {
        return $menu_objects;
    }

    // these is the list of things to hide.
    $items_to_hide = array(
        'Cart',
        'Wishlist',
    );

    foreach ( $menu_objects as $key => $menu_object ) {
        if ( ! in_array( $menu_object->title, $items_to_hide ) ) {
            continue;
        }

        unset( $menu_objects[ $key ] );
    }

    return $menu_objects;
}
add_filter( 'wp_nav_menu_objects', 'wpse_unset_menu_items', 10, 2 );

Modifying this to check something other than the title or to check for Jessica should be straightfoward

Magic Numbers

Avoid hardcoding post IDs such as 666! If the page gets deleted by accident it'll reappear in the menu as the ID will change when its recreated. It also means the code can only be used on 1 site, and even a migration to another host could break it!

2
  • Thanks Tom. I am either doing something wrong, or the above example isn’t working. Would you mind making it simpler and more specific to meet the objective out of the scenario? (ii) I am not hardcoding the post ID I just used the number 666 as an example. Commented Jul 3 at 13:53
  • the example is already super close to what you need, I added a comment or two to make it a bit easier to understand but there's very little that can be done to simplify it without fundamentally changing what it does. I also can't just do it for you and give you copypasta, understanding is necessary
    – Tom J Nowell
    Commented Jul 3 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.