Hide menu unless logged in

Tutorial 5 posts from 2 participants
  • Started 4 years, 8 months ago ago by Daryl I.
  • Replied 4 years, 8 months ago ago by Constantin
Viewing 6 posts - 1 through 6 (of 6 total)
  • #2331

    Daryl I.

    First off, this is a blessing for me…

    I use this when I build new sites – especially when they have sample data and code built-in.

    Under normal circumstances you have to change the default (demo) menu for your own and lose the demo nav processes to the building sections. With this plugin I can simply add the primary (demo) nav to the floating menu, and in the primary area build my custom menu.

    However, the floating menu is always available (not that that’s bad) but for my purposes, I have to turn the menu off/on when not building.

    Is there a way to have it so that when the admin is [ logged_in ] the menu displays, and if not, its hidden?

    #2341

    Constantin

    Hi,

    Please update to version 1.1.4 and place this piece of code into your WordPress theme functions.php file:

    
    /**
     * Remove CodeFlavors menu for logged in users
     */
    function remove_menu_logged_in(){
        return !is_user_logged_in();
    }
    add_action('cfm_show_menu', 'remove_menu_logged_in', 20);
    

    Edit: the code above will hide the menu for logged in users and show it to anonymous (not logged in) users.

    The filter is newly added into the plugin and allows you to hide the menu under certain circumstances that you determine for yourself into the filter callback (in this case, if the user isn’t logged in).

    #2344

    Daryl I.

    OK so I upgraded to 1.1.4 and added the function and it works in the opposite manner… odd.

    I tried turning the “Hide it” checkbox on and off but that didn’t work either..(what’s that for anyway?)

    I tried swapping the ins for outs in the code, but all I got with that were errors showing in the header.

    Any other ideas?

    #2345

    Constantin

    Sorry, my bad, read things wrong. I understood you wanted to hide the menu for logged in users when it was the other way around. Please replace the code in your theme functions.php with this one:

    
    /**
     * Remove CodeFlavors menu for logged in users
     */
    function show_menu_logged_in(){
        return is_user_logged_in();
    }
    add_action('cfm_show_menu', 'show_menu_logged_in', 20);
    

    Please note that the above will display the menu to all logged in users, no matter their role.

    If you want to display the menu ONLY for admins, please use this piece of code:

    
    /**
     * Remove CodeFlavors menu for logged in admins
     */
    function show_menu_admin_logged_in(){
        return current_user_can( 'manage_options' );
    }
    add_action('cfm_show_menu', 'show_menu_admin_logged_in', 20);
    
    #2347

    Daryl I.

    Now we’re cookin’ with gas….

    This is just what the doctor ordered, I have it on a site build right now and setup the regular menu in the main nav – with all the build pages in the floating – works great…

    Many, many thanks for this, to you…

    My regards…

    A thought occurs – you may want to add this functionality in a future release, for others to use without the need to “fiddle” with the functions file. Something like a checkbox in the settings, maybe…

    #2349

    Constantin

    Happy to hear everything works now. As for putting this into the plugin as an option, I don’t think it’s a good idea since menus should be visible at all times and this is a customization. Anyway, I’ll think about it.

    If it’s not much to ask for I would appreciate it if you could leave an honest review on the plugin reviews page, thank you in advance.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.