Support » Plugin: Nav Menu Roles » All menu items are displayed if you are not logged in

  • Resolved IndikatorDesign

    (@indikatordesign)


    I use the plugin for the first time and it makes a very good impression.

    But there is a problem if you are not logged in. I’m using the current Divi theme with WP 5.0.

    When you are logged in, the menu items saved for the main menu are displayed.

    If you are not logged in, all menu items are displayed at once. If I deactivate Nav Menu Roles, the normal main menu will be displayed even if I am logged out.

    Does anyone have any idea where this can come from? Thanks!

    http://7.ly/wwsk

Viewing 12 replies - 1 through 12 (of 12 total)
  • I’m seeing the same thing with the exact same setup Divi theme and WP 5.0

    Plugin Author helgatheviking

    (@helgatheviking)

    Have you tried switching to a default theme? If the problem goes away, then the issue is with Divi…. which seems likely since you both are using that theme. I don’t have access to that theme, so can only guess that they are doing something custom or perhaps filtering the items itself.

    IndikatorDesign

    (@indikatordesign)

    I don’t know why it happens, but I have written my own solution and that works very well:

    
    // filter hook inside the initialize function
    add_filter( 'wp_get_nav_menu_items', [ $this, 'hideMenu' ], 99, 2 );
    
    // The function itself
    /**
     * Hide menu items for different roles
     *
     * @since   1.0.0
     */
    public function hideMenu( $items, $menu )
    {
    
        $index  = 0;
        $remove = [];
    
        foreach ( $items as $key => $item ) :
    
            switch ( $item->title ) :
    
                case 'Title 1' :
    
                    if ( ! get_user_meta( wp_get_current_user()->ID, 'my_meta_key', true ) )
                        $remove[] = $index;
    
                    break;
    
                case 'Title 2' :
                case 'Title 3' :
    
                    if ( ! current_user_can( $this->whatever ) )
                        $remove[] = $index;
    
                    break;
    
            endswitch;
    
            $index++;
    
        endforeach;
    
        foreach ( array_reverse( $remove ) as $key => $val ) :
    
            unset( $items[$val] );
    
        endforeach;
    
        return $items;
    
    } // end hideMenu
    
    Plugin Author helgatheviking

    (@helgatheviking)

    hi @indikatordesign. could you send me a screenshot of your menu and a screenshot of the output? Also, could you verify that the problem persists in a default theme such as Twenty Seventeen.

    Plugin Author helgatheviking

    (@helgatheviking)

    Hi @elliotbetancourt, would you send me a screenshot of your menu configuration? (ie, the admin side)

    @indikatordesign I’m glad you’re able to resolve it with that code. Switching on the title is unfortunately not something I can rely on in the plugin core as it doesn’t account for multilingual sites. Nor does the plugin check for user meta.

    Question: Are all the menu items configured to display for a logged in user? Is there any menu item that is meant to display to logged out users?

    If all the menu items are hidden for a user, the menu will sometimes default to the page menu. I got a copy of Divi and while wp_nav_menu doesn’t default to the page menu as a fallback, it does have some code that will print a custom page menu if the main menus are null… essentially the same thing. And well, your video looks like a page menu is displaying.

    Please double-check that at least 1 menu item is configured to display to logged out users and/or ‘everyone’.

    Until someone can confirm this is occuring with a default theme, I’m gonna stay fairly convinced it’s an issue with Divi. Divi and possibly also your menu config.

    Cheers!

    Plugin Author helgatheviking

    (@helgatheviking)

    I can confirm that Divi is adding pages when a wp_nav_menu() is empty.

    Partial Menu Config:
    https://cl.ly/0bb0b2059840
    (ALL menu items displayed to logged in users (either all or by role)

    Front-end Logged out user view
    https://cl.ly/ecac780af725
    Note: you see all the pages on the site, as mentioned in the original topic

    Tweaking the Divi settings, apparently there’s no way to disable this entirely and you have to do it page by page:

    Divi Theme Settings, All pages excluded.
    https://cl.ly/edc4825c3d2d
    (not seen, home link disabled, and Uncategorized also excluded)

    Results (no pages displayed for logged out user)
    https://cl.ly/c94dfae35afc

    So you can modify the Divi settings. Or you could also modify the divi theme’s header.php (in a child theme!) to not include the page menu. Or I believe the easiest fix, will be for you to ensure you have 1 menu item that will be shown to logged out users.

    Best of luck. And if you’ve found my support helpful, please consider leaving a donation for my handball team:
    https://www.paypal.com/fundraiser/110230052184687338/charity/1451316

    IndikatorDesign

    (@indikatordesign)

    Hello helgatheviking,

    sorry for the late answer. I have used “Home”, “Shop” and “My Account” for all users in the main menu, so this does not seem to be the problem. Maybe in my case the problem is that I am working with a multisite environment with a folder structure. Or maybe it’s really a conflict with Divi itself.

    In my case, I can work in my own solution with the titles because I use different sites for different languages. It’s the cleanest and most powerful solution. I don’t like multilingual plugins because they slow down all processes and they causing a lot of problems.

    So for me it’s the best way to work with my own solution and extend it as needed. So you can mark the contribution as solved for me. Unless of course “elliotbetancourt” still has questions.

    Best regards, Bruno

    Plugin Author helgatheviking

    (@helgatheviking)

    Thanks for the feedback, Bruno.

    Multisite Compatibility was added in 1.9.0, so double check you are using the latest version.

    The test for logged IN users:

    
    $visible = is_user_member_of_blog() || is_super_admin() ? true : false;
    

    The test for logged OUT users:

    
    $visible = ! is_user_member_of_blog() && ! is_super_admin() ? true : false;
    

    So in the case of multisite, the user must be logged in AND a member of the current site.

    Unfortunately, I don’t have time to re-test multisite compatibility right now, if you’re happy with your custom version, that’s totally fine with me! 🙂

    Cheers!

    elliotbetancourt

    (@elliotbetancourt)

    @helgatheviking I just figured out a fix. Previously, it was enough to set a top-level menu item to only be visible to a logged-in user, and all the items under it (in the dropdown) would be automatically hidden. In the latest version, you have to manually set all the items underneath to also only be visible to a logged-in user, or they will all show at the top-level.

    Plugin Author helgatheviking

    (@helgatheviking)

    @elliotbetancourt

    In the latest version, you have to manually set all the items underneath to also only be visible to a logged-in user, or they will all show at the top-level.

    Do you mean the version I released yesterday? 1.9.3? I hope I didn’t break anything, because I did not intentionally change that behavior, but I only did a few quick tests. A hidden item should automatically hide it’s children.

    elliotbetancourt

    (@elliotbetancourt)

    @helgatheviking No, I think it was probably the latest version (or a recent version) of Divi that caused the issue. I was seeing this issue before your release from yesterday.

    Plugin Author helgatheviking

    (@helgatheviking)

    Ok, good to know. The test version I found is probably a little old, but Divi causes me no end of problems for every plugin I write.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘All menu items are displayed if you are not logged in’ is closed to new replies.