In my wordpress page, I add Login- logout item to the main menu using the code (in functions.php) below :

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

When login is performed using this button, It should redirect the current page where login is clicked. How can I do that?

I think wp_loginout('index.php') needs to change because after login and logout I redirect to home page. What should I write to go previous page ?

1 Answer 1


wp_loginout accepts two arguments, $redirect (string) and $echo (boolean). You can specify the URL of the page to redirect to after logging in or out with the $redirect parameter. In your current code, $redirect is 'index.php'.

You can get the full URL of the current page by using

( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']


wp_loginout( ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );

That should do the trick!

  • Engelen thanks for your answer.But that line of code you provide me gets this error :/ Parse error: syntax error, unexpected T_STRING in /home/u935918480/public_html/wp-content/themes/twentytwelve/functions.php Jun 10, 2014 at 18:50
  • remove the single quote before the last $_SERVER statement
    – leromt
    Jun 10, 2014 at 19:13
  • Sorry about that, I've corrected it in my answer! Thanks @leromt.
    – engelen
    Jun 10, 2014 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.