Viewing 8 replies - 1 through 8 (of 8 total)
  • Try this (I’ve modified/enhanced your code a little):

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
    
    	if($args->theme_location == 'primary-menu') {
    		if(is_user_logged_in()) {
    			$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    		}
    		else {
    			$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    			$items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
    		}
    	}
        return $items;
    }
    Thread Starter sillydwa

    (@sillydwa)

    I added the code as follows

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
    
    	if($args->theme_location == 'primary-menu') {
    		if(is_user_logged_in()) {
    			$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    		}
    		else {
    			$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    			$items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
    		}
    	}
        return $items;
    }

    it has made the login / Logout Link disappear and all that is shown is the Register link.

    Weird?!

    Let’s go back to your original code, then:

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
        if (is_user_logged_in() && $args->theme_location == 'primary-menu') {
            $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
        }
        elseif (!is_user_logged_in() && $args->theme_location == 'primary-menu') {
            $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
            $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
        }
        return $items;
    }

    Thread Starter sillydwa

    (@sillydwa)

    ok so this particular code is working for the login and logout

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
            ob_start();
            wp_loginout('index.php');
            $loginoutlink = ob_get_contents();
            ob_end_clean();
            $items .= '<li>'. $loginoutlink .'</li>';
        return $items;
    }

    how would i modify this to show register if the user is logged out and not if the user is logged in.

    I made a boo boo and posted code from an old site originally I apologize for the confusion

    Hmm… I don’t like the ob_start() method…

    One sec…

    Okay, try this one:

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
    
    	if($args->theme_location == 'primary') {
    		if (is_user_logged_in()) {
    			$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    		}
    		else {
    			$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    			$items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
    		}
    	}
        return $items;
    }

    The problem was the primary-menu should be just primary.

    Thread Starter sillydwa

    (@sillydwa)

    Absolutely Beautiful!!! That worked!!! You my friend are awesome 🙂 thanks for the quick help!!!

    Lol… you’re very welcome 🙂

    Have fun!!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Register link’ is closed to new replies.