WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
up vote 1 down vote accepted

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!

share|improve this answer
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 – Cagkan Toptas Jun 10 '14 at 18:50
remove the single quote before the last $_SERVER statement – leromt Jun 10 '14 at 19:13
thanks @leromt now it is working – Cagkan Toptas Jun 10 '14 at 19:26
Sorry about that, I've corrected it in my answer! Thanks @leromt. – engelen Jun 10 '14 at 19:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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