• Resolved gamicord

    (@gamicord)


    I want to hide an item from my Menu, if a user is logged out.

    So my Menu has | Home, Listings, Store Manager, About | — as the Menu Items.

    I will like “Store Manager” to be hidden when the user is logged out, and made visible on the Menu when the user is logged in.

    Currently, I believe that the below code will achieve this for me.

    But it relies on me creating and using Two(2) separate Menus.

    To save myself from duplication, I would like to only have to manage One Nav Menu.

    Here’s the Code I presently have:

    function my_wp_nav_menu_args( $args = '' ) {
    
    if ( is_user_logged_in() ) { 
        $args['menu2'] = '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 am aimed at going— where I have Two(2) Nav Menus, and would actually want to have just One(1) Nav Menu?

    2.) It was suggested somewhere that CSS can do it.

    But I don’t like the idea of just hiding Menu items with CSS as they would still be visible using the code inspector on the page.

    I am looking for a more secure way of removing menu items, without being able to still see them with the Page inspector on the frontend.

    Anyone with a better code that can help me achieve this?

    Regards.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Lets try the below code:

    function hide_menu_conditional($items, $args) {
        // Check if the menu location is your primary menu
        if ($args->theme_location == 'primary-menu') {
            // Check if the USer is logged in
            if (is_user_logged_in()) {
                // If the user is logged in , find the menu item with the title 'Store Manager'
                foreach ($items as $key => $item) {
                    if ($item->title == 'Store Manager') {
                        // Remove the menu item
                        unset($items[$key]);
                        break;
                    }
                }
            }
        }
    
        return $items;
    }
    
    add_filter('wp_nav_menu_objects', 'hide_menu_conditional', 10, 2);
    
    Thread Starter gamicord

    (@gamicord)

    @phillsav

    I have applied the code on my website as you can see here— https://prnt.sc/AKyhP3TOXPBv

    Unfortunately, I don’t think it worked. It doesn’t seem like its doing anything on my website.

    The Menu Item “Store Manager” is still visible to Logged out users as you can see here– https://prnt.sc/PNzhT2M1-820

    THE REQUEST
    When Logged out, the Menu Item “Store Manager” should be hidden.

    When Logged in, the Menu Item “Store Manager” should be visible.

    That’s what I’m looking to achieve.

    Regards.

    Thread Starter gamicord

    (@gamicord)

    @rohitmukati

    Thanks for this suggestion.

    The thing is that I like to reduce the number of Third Party plugins on my website.

    Apart from compatibility issues, those Third Party plugins plague someone with too much adverts on the Wp-Admin Dashboard.

    So for things that are related to WP Core like hiding Nav Menu item, blocking access of Wp-Admin and Wp-login.php Pages from logged out users, or blocking access to all Buddypress Pages from Logged out users, and other WP Core related components and matters, I like to go through the code solution— and save myself the burden of being bombarded with too many adverts from third party plugin Authors on my WP dashboard.

    The code solution is better for me– because PHP in WordPress will always be compatible with itself.

    But Third Party plugins can pose compatibility issues with my Theme and other plugins on my website.

    This is just the reason I go “Code First” then “Plugin later“.

    But when all code attempts have failed, and there’s an existing plugin that meets that need, then I go for the plugin.

    Now, I’ve taken note of the plugin you suggested (even though I was pre-aware of it.)

    I will later go for it, if all WordPress Forums, Google Search, Stack Exchange and Stack Overflow cannot provide me this code I’m looking for.

    I suppose you understand?

    Regards.

    Hi @gamicord

    Thank you for the feedback. Looks like you have applied it correctly.

    I’ll recheck the php code when I get a minute, in the meantime if you find a solution please close the ticket.

    Thread Starter gamicord

    (@gamicord)

    @phillsav

    You haven’t had time yet to review the code?

    Hi @gamicord

    I haven’t tested it the original code, however the logic looks ok. I guess it may have unreliable user login checks in ‘wp_nav_menu_objects’ filter.

    You could try to check user login status in the ‘wp_nav_menu_objects’ filter, hook into ‘init’ or ‘template_redirect’ to seta global variable indicating login status, then access that variable within the filter function.

    This way we could make sure the users login status is correctly determined before using ‘unset’ to hide the ‘Store Manager’ menu item.

    Hello @gamicord
    Try this code this will work for you.
    Code is below

    function hide_menu_conditional($items, $args) {
            // Check if the USer is logged in
            if (!is_user_logged_in()) {
    
                // If the user is logged in , find the menu item with the title 'Store Manager'
                foreach ($items as $key => $item) {
                    if ($item->title == 'Store Manager') {
    
                        // Remove the menu item
                        unset($items[$key]);
                        break;
                    }
                }
            }
    
        return $items;
    }
    
    add_filter('wp_nav_menu_objects', 'hide_menu_conditional', 10, 2);
    • This reply was modified 1 year, 2 months ago by rohitmukati.
    Thread Starter gamicord

    (@gamicord)

    Phill (@phillsav) , rohitmukati (@rohitmukati)

    Considering the problem of the code not working, I tried to simplify my problem and create a better context.

    This is now the current situation:

    On my Menu, I have Login and Logout Menu items.

    The Login link is an Elementor created Page.

    While the Logout is an endpoint supplied by BuddyPress.

    I have enabled CSS Classes in my Menu through Screen options.

    So the Login link has a CSS Class of [ buddyx-elementor-login ] while the Logout link has a CSS Class of [ bp-menu bp-logout-nav ]

    If people Login, I want the Login on the Menu to be hidden– because they are already logged in.

    And if they Logout, I want the Logout on the Menu to be hidden– because they are already logged out.

    Can you Please adjust the PHP Code Snippets to then use these CSS Classes to do the dynamic Hide and Show?

    Regards.

    Hi @gamicord,

    A simple way to achieve this would be to create CSS classes using the ones mentioned. Use WordPress’s dynamic body classes (logged-in and logged-out) in your CSS to style elements differently based on whether the user is logged in or logged out.

    Thread Starter gamicord

    (@gamicord)

    Phill(@phillsav)

    I believe WordPress will always add the body class.

    With the assumption that WordPress always adds the body class, can you Please just send me the required CSS Codes?

    Regards.

    Thread Starter gamicord

    (@gamicord)

    I guess I’ve found the required codes, and it’s working now.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Hide One or More Nav Menu Item from Logged out user’ is closed to new replies.