Guest only menu items

I have posted before about a menu CSS snippet I found to only show menu items to logged in users. I have been looking for a suitably elegant way to reverse this and have a menu item ONLY visible to users who are NOT logged in.

Today I have arrived at a solution. This is not the only solution, and by all means, it’s not the best. But for my needs, this is perfect (see notes in the Conclusion).

This needs to be done in three parts and this will give you the option to have menu item(s) displayed for ONLY logged in users, as well as having them displayed for ONLY guests. The choice is yours and can be changed at anytime easily in the dashboard of wordpress single blog sites, or blogs a part of the multi-site setup–although some of the setup process might not be available to you in a multi-site setup if you are not the super-admin.

Step 1 — adding code to functions.php

Insert the following code into your functions.php file:

add_filter('body_class','not_logged_in_class');

function not_logged_in_class($classes = '')
{
	if ( !is_user_logged_in() ) {
		$classes[] = 'not-logged-in';
	}

	return $classes;
}

Step 2 — adding style to your theme’s CSS

Add the following CSS to your style sheet:

#menu-mainmenu li.logged-in-nav {
display: none;
} 

body.logged-in #menu-mainmenu li.logged-in-nav {
display: list-item;
}

body.not-logged-in #menu-mainmenu li.logged-out-nav {
display: list-item;
}

#menu-mainmenu li.logged-out-nav {
display: none;
}

IMPORTANT: The menu I use for my primary menu is called “MainMenu” ergo my CSS markup above includes “#menu-mainmenu”. Where I originally obtained the inspiration for this source used “#nav”. It may be prudent to first determine what your theme throws out as an ID for the navigation menu. Just check the page source and find out what it is. You want the name of the ID associated with the unordered list that is associated with the list items of where your menu is to be displayed. For example, if you named your primary menu “MyPrimaryMenu”, you should change the above code to “#menu-mymainmenu”. Values I have seen from various blogs include: #nav, #menu, #menu-MENUNAME (without commas of course). DON’T FORGET TO CHANGE THIS.

Step 3 — using the CSS

Create your page, and make sure it appears in your menu. Drag it to the desired location within the menu structure and open the the drop down to the right (downward pointing triangle). On the right under “Title Attribute” there is a text field with the heading “CSS Classes (Optional)”, insert “logged-in-nav” or “logged-out-nav” as appropriate into this box, and save the menu. For something to be visible to only logged-in users, use “logged-in-nav”. For something to only be visible to guests, use “logged-out-nav”.

That’s it! You’re done!

Since the object of this exercise was to have a menu item displayed for only guests, let’s test it out now. View your blog/website and confirm that the menu item is NOT there when you’re logged in. Log out, and check that it DOES appear. If it’s not behaving as expected go back and check your steps. Also, there are some quick things to check listed below in Troubleshooting.

Caveat and conclusion:

Once implemented, this setup gives a simple way to chop and change menu items visibility dependant on whether a user is logged in or not. This has not been tested in an environment where user access management is in place. I have tried various plugins for access management, but none gave me the option to SHOW ONLY to guests.

WARNING: There is one downside to this method. The end-user does not SEE the menu item, because the CSS “hides” it from them. It doesn’t altogether remove it from the page source and screen readers may in fact read the menu item–I have no personal knowledge on this. Anyone viewing the source will see these links — whether logged in or not. So unless you have the destination pages “controlled” in some other way, then maybe this method isn’t the best solution. This suits my purposes, but it might not suit yours.

I hope you find this snippet as useful as I did.

Troubleshooting:

  • Make sure you changed #menu-mainmenu to the appropriate ID name of the unordered list holding the list items for your menu!!! <– this is the most likely cause of it not showing up;
  • Clear the cache for your browser! Should be done first, you don’t want to spend the next hour debugging to find there’s no error! It’s possible the new style wasn’t loaded;
  • Check your spelling! A single simple typing mistake could be to blame, go and double check your work;
  • Make sure you’ve made the appropriate changes in the two files as required and SAVED/UPLOADED to the correct place;
  • If it’s still not there, make sure you’ve edited the correct theme’s files;
  • It could also be the fact your style sheet is not loading, try modifying some other part of the CSS and see if it changes in your browser.

Other notes and advice:

  • You can modify your theme’s style sheet and functions.php file directly–not ideal because subsequent style updates by the theme developer will see your style go out the window;
  • You can create a child theme of your current theme and make the changes there–create the functions.php file and add the code, as well as the styles to the CSS. This is a lot better, and the preferred method when dealing with a fixed theme, but this style and function will be lost when you change the theme;
  • Some theme’s have a CSS injection mechanism which is more ideal, but this also suffers from the lack of carry over to any new and subsequent theme;
  • Finally (and the way I have done it), I found a simple plugin years ago that allows custom CSS to be added and kept across all your themes (for that blog). The plugin allows you to save the CSS in the wp database and is not tied to any theme.

References:


This entry was posted in general and tagged , , , , . Bookmark the permalink.

8 Responses to Guest only menu items

  1. Pingback: Showing menu items to logged in users only | dav3 on wordpress

  2. Pingback: DCS Enterprise | Guest only menu items | dav3 on wordpress

Leave a Reply to Tony M Cancel reply

Your email address will not be published. Required fields are marked *