Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to implement a conditional menu destination in WordPress

Posted on 2016-07-24
9
Medium Priority
?
92 Views
Last Modified: 2016-07-29
I have a standard menu at the top of my WordPress pages and I'd like the "Home" tab to go one place if the user is logged in and to another place if the user is not logged in.

Thanks for any ideas.
0
Comment
Question by:steva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:William Nettmann
ID: 41728198
I have used this snippet to do something similar:
function add_login_logout_register_menu( $items, $args ) {
	if ( $args->theme_location != 'primary' ) {
		return $items;
	}
 
	if ( is_user_logged_in() ) {
		$items .= '<li><a href="' . wp_logout_url() . '">' . __( 'Log Out' ) . '</a></li>';
	} else {
		$items .= '<li><a href="' . wp_login_url() . '">' . __( 'Login In' ) . '</a></li>';
		$items .= '<li><a href="' . wp_registration_url() . '">' . __( 'Sign Up' ) . '</a></li>';
	}
 
	return $items;
}
 
add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 199, 2 );

Open in new window

You will need to add a "Home Page" link to a custom menu using the Appearance -> Menus option in WordPress, and use a placeholder instead of a URL, e.g. "#dynamic-link". You can then do a "str_replace" on the string in the "$items" string in the function instead of adding the login / logout link at the end.
0
 

Author Comment

by:steva
ID: 41732471
Hi William,

It seems that what I want to do in my filter function is find the "Home" li in $items and then change its href to the new "Home" if ( is_user_logged_in()).    Is it clear to you how to do that?

Thanks
0
 
LVL 12

Expert Comment

by:William Nettmann
ID: 41732505
Yes, it is clear.

Give the "Home" link a placeholder URL, e.g. "##home##"

In the filter:
$real_url = is_user_logged_in() ? "http://example.com/logged-in" : "http://example.com/logged-out";
$items = str_replace("##home##",$real_url,$items);
return $items;

Open in new window

Job done.
0
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 

Author Comment

by:steva
ID: 41733416
Thank you!  I looked hours last night for documentation on how to bust open the $items objects and work with them,  and finally I thought, "You, know?  I'll bet William could do this in about a minute!"

You're the best!

But it's still not clear to me exactly what you're doing.  The first parameter to str_replace is the thing to search for and you've got "##home##".   How does  that find the highlighted <li> element below and how does the $real_url filled in line 1 work as a replacement for that in the menu?

 Thanks

Steveli element to replace
0
 
LVL 12

Accepted Solution

by:
William Nettmann earned 2000 total points
ID: 41733507
How are you configuring your menu?

I am guessing you are going to "Appearance - > Menus" and then adding pages to the menu.

Instead of adding the home page as a menu item, add  a Custom Link with "##home##" as the URL, and "Home" as the label.

The "str_replace" then finds the "##home##" in the $items string (it is not an object) and replaces it with $real_url.

If you can't add the Custom link, use "http://big-black-block.staging.wpengine.com/" instead of "##home##" in the str_replace.
0
 

Author Comment

by:steva
ID: 41734027
Got it!  I ended up with:
Dynamic Menu 1Dynamic Menu 2
Many thanks!
0
 

Author Closing Comment

by:steva
ID: 41734028
Thank you again.  A very useful tool to add to my toolbox!
0
 
LVL 12

Expert Comment

by:William Nettmann
ID: 41734488
Pleasure, Steve.

Looking at your screenshot above, you could have just added two links to the menu, one for logged in users and one for logged out users.
0
 

Author Comment

by:steva
ID: 41735096
Yes.  Put in two menu tabs, both with the "Home" navigation label, and have one go to "Homepage" and the other gp to "login_home."  Then click "Logged In Users" for one and "Logged Out Users" for the other in Display mode. Thanks for that.
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question