Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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) {
        ob_start();
        wp_loginout('index.php');
        $loginoutlink = ob_get_contents();
        ob_end_clean();
        $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

1 Answer 1

up vote 0 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']

yielding

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

Your Answer

 
discard

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.