I must be having another senior moment. The following code has been working fine for me for -years-. I applied it to a site using 4.9.2 with a child theme of Twenty Seventeen and it fails in a peculiar way.

See the comment line:

// should be <a href="http://example.com/login/">Login/Register</a>

The concatenation directly before that returns -only- 'Login/Register' and -not- the URL of the web site. Although that function -is- correctly showing the user is logged in. Something is going wrong with the -concatenation-.

What is weird is that it's not the PHP version because I have other WP sites running on the same server and they run just fine with the same code.

What am I missing?

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {

$x = str_replace( 'menu-item-3667', 'menu-item-login', $items);

$loginlink = '<a href="' . get_site_url() . ' ">Login/Register</a>';
// should be <a href="http://example.com/login/">Login/Register</a>
// but instead returns only 'Login/Register' with empty spaces where the remainder of the string (the URL and the <a> tag) should be.

if(  is_user_logged_in() ) {
 global $current_user;
 $loginoutlink =  '<a title="You are logged in as: ' . $current_user->display_name . '" href="' . wp_logout_url( home_url() ) . '">Log Out</a>';
 $loginlink = '<a href="' . get_site_url() .  '/login/">Logout</a>';
 $x = str_replace( $loginlink, $loginoutlink, $x);
 $loginoutlink = '<a href="/login">Log In/Register</a>';
 str_replace( $loginlink, $loginoutlink, $x);

return $x;
  • Your code is disappointing. Check the logics. – Max Yudin Mar 6 at 19:07
  • In what manner? – jchwebdev Mar 6 at 19:22
  • For exapmle, you redefine $loginlink. Why? Just give $x the right value. No replaces, just straight string. – Max Yudin Mar 6 at 19:41
  • `if( is_user_logged_in() ) {one string}else {another_sting} } – Max Yudin Mar 6 at 19:45
  • This is NOT related to WordPress. I'll mark it as off-topic. But I'll try to helpp you. – Max Yudin Mar 6 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.