WordPress.org

Ready to get started?Download WordPress

Forums

Nav Menu Roles
[resolved] Show menu for logged out AND by role (11 posts)

  1. adam1234567
    Member
    Posted 4 months ago #

    How can I show a menu item for both logged out users AND by role?

    Here is an example. I have a job board. I'd like to show the menu items Post a Job and Post a Resume for logged out users. For logged in Subscribers I'd like to show Post a Job and hide Post a Resume. For logged in Candidates I'd like to show Post a Resume and hide Post a Job.

    I can't seem to be able to select by "By role" and logged out at the same time.

    https://wordpress.org/plugins/nav-menu-roles/

  2. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    You can't. The plugin just isn't set up for that. Best case would be to create 2 menu items for "Post a Job": the first, only shown to logged out users and the second show to Candidates.

  3. adam1234567
    Member
    Posted 4 months ago #

    ah i didn't even think of that. very logical :) thank you!

  4. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    It isn't really ideal, but yours seems a special use case and I'm probably not updating the plugin to change how it works any time soon.

  5. Jason Resnick
    Member
    Posted 4 months ago #

    I have a similar case.

    Where I have a WooCommerce store and I'd like to show a link for Join for those users that don't have an active Subscription, but also those that aren't logged in as well.

  6. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    I'm open to suggestions. I just don't want to have all checkboxes because then people could easily check 'all logged in' and 'all logged out' and it would be a mess.

  7. Jason Resnick
    Member
    Posted 4 months ago #

    That I totally get for sure.

    I'm totally shooting off the top of my head right now, but maybe if 'By Role' is selected then there are radio buttons for Logged In/Logged Out/Both and then you have the roles listed as checkboxes?

  8. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    What about adding "Logged Out" as a pseudo "role" and giving it a checkbox?
    http://i.imgur.com/M686iYe.png

    I don't know how that would play with the back-end code however. As a friendly reminder that this code is on Github and I am open to contributions.

  9. Jason Resnick
    Member
    Posted 4 months ago #

    I think that may work -- I had forked it and working on a solution that seems to work well.

  10. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    Send me a pull request if you come up with something good.

  11. helgatheviking
    Member
    Plugin Author

    Posted 4 months ago #

    It turns out that this is already possible using the current filters available in NMR. Modified from the FAQ and taking into account @CodeWaggle's correction to the FAQ (to be published soon) here is my suggestion:

    function kia_new_roles( $roles ){
    	$roles['logged-out'] = 'All Logged Out';
    	return $roles;
    }
    add_filter( 'nav_menu_roles', 'kia_new_roles' );
    
    function kia_item_visibility( $visible, $item ){
        if( isset( $item->roles ) && is_array( $item->roles ) && in_array( 'logged-out', $item->roles ) && ! is_user_logged_in() ){
    		$visible = true;
    	}
    	return $visible;
    }
    add_filter( 'nav_menu_roles_item_visibility', 'kia_item_visibility', 10, 2 );

    I make no guarantees and can't really work on it further. It seems to work for me, but hasn't been extensively tested either.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.