• Resolved tech55541

    (@tech55541)


    Hello all,
    I would like to add a login logout link to my menu. I have used this code for my footer link, but do not know how to make this work with the menu.

    <?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . wp_logout_url( get_permalink() ) . '" title="Logout">Log Out</a>';
    } else {
        echo '<a href="' . wp_login_url( get_permalink() ) . '" title="Login Or Register">Log In</a>';
    }

    The trick is I would like the login and logout links to take the user back to the page they were previously on when they clicked the login link.
    If it is possible to only do this for subscribers, that would be even better.

    Any suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter tech55541

    (@tech55541)

    OK, so I found this code, but I still cannot figure out how to make it redirect to the current page they were on when clicking login or logout. I think it is the get_permalink function, but I don’t know where to add it in.

    /***Login Logout link on menu***/
    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; }
    Thread Starter tech55541

    (@tech55541)

    Anyone have any ideas?

    Thread Starter tech55541

    (@tech55541)

    Topic Resolved
    All you have to do is add this code.

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2); 
    
    function add_login_logout_link($items, $args) {
    	$pageURL  = 'https://';
    	$pageURL .= $_SERVER['HTTP_HOST'];
    	$pageURL .= $_SERVER['REQUEST_URI'];
    
             ob_start();
    	 wp_loginout($pageURL);
             $loginoutlink = ob_get_contents();
             ob_end_clean();
             $items .= '<li>'. $loginoutlink .'</li>';
         return $items;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Login link to menu’ is closed to new replies.