What I have:
I have a log-in/log-out link in a standard WordPress navigation menu. So far, I’ve conditionally filtered the URL to either the necessary log-on or log-out urls.
What I need:
I also need to conditionally change the textual value of a link (menu item) in the navigation menu.
My code:
add_filter( 'nav_menu_link_attributes', 'menu_override_b', 10, 3 );
function menu_override_b( $atts, $item, $args ) {
if ( is_user_logged_in() ) {
$url = wp_logout_url();
$newlink = str_replace("http://--loginout--", $url, $atts[href]);
$atts[href] = $newlink;
//None of the following work...
/*
$title ="Logout";
$atts[title] = $title;
$atts[post_excerpt] = $title;
$atts[description] = $title;
$atts[attr_title] = $title;
$atts[post_title] = $title;
$atts[post_content] = $title;
*/
}
else{
$url = "/somewhere/else";
$newlink = str_replace("http://--loginout--", $url, $atts[href]);
$atts[href] = $newlink;
//None of the following work...
/*
$title ="Login";
$atts[title] = $title;
$atts[post_excerpt] = $title;
$atts[description] = $title;
$atts[attr_title] = $title;
$atts[post_title] = $title;
$atts[post_content] = $title;
*/
}
return $atts;
}
I know it’s an old post but in case anyone’s looking…
You need to replace
$atts[href]
with$atts['href']
in your code.I use this really nice plugin from here
https://wordpress.org/plugins/menu-items-visibility-control/
This allows you to set a call back to conditionally hide menu items, what I would do is have a menu item for when they are logged in and another for when they are logged out, and just swap them.
I’d have to look up the exact code you would need for your callback, but It should go in the theme functions file, then you just run that function on the navigation items, as per the plugins instructions.
It’s a bit of a different approach then you are currently doing, but it might be a bit cleaner to keep the navigation items separate and fully functional ( not reliant on external code for their content, only their visibility )
It’s also very easy to expand this to other links,
Hope it helps.
–update looking at it, it might have the functionality you need already built in. I use it for product subscriptions from a third party app etc.. but same idea really.