Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Redirect Wordpress users to their own site on logout

As I'm sure everyone knows that when a user is created from the backend of Wordpress, there is the option to add a website URL. This is the URL I want the user to be redirected to when logging out of a Wordpress website.

E.g. User logs out of site, they're redirected to the URL associated with their user account. If no URL exists just redirect to the hime page of the site they are logged into.

Any help would be appreciated - any questions, please ask.

User Gravatar

ddd-h

Posted Feb 9 2014 5:15 UTC

$30


  • Expired
  • php
    wordpress
  • 8162 Views

16 Replies


Hi Dave,

you need to add an action to the wp_logout action in your functions.php. You can use the user_url field from the user's profile to redirect users.

Here's a sample code:

add_action("wp_logout", "my_logout");
function my_logout(){
    $userinfo = get_currentuserinfo();

    if($userinfo->user_url){
        wp_redirect($userinfo->user_url);
        die();
    }

    wp_redirect(home_url());
    die();
}
User Gravatar

fulippo

Posted Feb 9 2014 6:44 UTC

I will try this out when I get back home.

Thanks for your response.

User Gravatar

ddd-h

Posted Feb 9 2014 9:07 UTC

Hi Dave,

did it solve you problem?

User Gravatar

fulippo

Posted Feb 10 2014 12:49 UTC

Hmm, the wp_logout filter is triggered after the wp_clear_auth_cookie() call, in which case I am not sure get_currentuserinfo() will still work.

My suggestion is to set the redirect URL for logout, upon user log in in a cookie, and then apply the URL to the wp_logout_url as the redirect_to parameter:

<?php 

/**
* Sets the custom cookie with the redirect URL
*/
function usersite_set_logout_url_cookie() {

    if( !is_user_logged_in() ) return false;

    $exists = $_COOKIE['the_logout_url'];

    if( empty( $exists ) ) {

        $userinfo = get_currentuserinfo();

        if( $userinfo and isset( $userinfo->user_url ) ){
            $expire = time() + ( 86400 * 7); @setcookie('the_logout_url', $userinfo->user_url, $expire);
        }

    }


}
add_action( 'init', 'usersite_set_logout_url_cookie');

/**
 * Applies the URL saved in the cookie to the wp_logout_url for redirect
*/
function usersite_logout_redirect_url( $logouturl ) {

    $redirect = $_COOKIE['the_logout_url']; 

    if( $redirect ) //redirect to the custom set url
        return $logouturl . '&amp;redirect_to=' . urlencode( $redirect );

    //redirect to homepage
    return $logouturl . '&amp;redirect_to=' . urlencode( get_option( 'siteurl' ) );

}
add_filter('logout_url', 'usersite_logout_redirect_url', 10, 2);
User Gravatar

adrian7

Posted Feb 13 2014 2:24 UTC

OMG, I realized my code above could be greatly improved, just by adding the user's site URL directly to logout_url hook:

/**
 * Applies the URL saved saved in user profile fields to the redirect url upon logout
*/
function usersite_logout_redirect_url( $logouturl ) {

    $userinfo = get_currentuserinfo();

    if( $userinfo and isset( $userinfo->user_url ) ){ //redirect to the custom set user's url
        return $logouturl . '&amp;redirect_to=' . urlencode( $userinfo->user_url );

    //redirect to homepage
    return $logouturl . '&amp;redirect_to=' . urlencode( get_option( 'siteurl' ) );

}
add_filter('logout_url', 'usersite_logout_redirect_url', 10, 2);

Hope it helps.

User Gravatar

adrian7

Posted Feb 13 2014 2:33 UTC

So to conform what should I be pasting into my functions.php file?

I'll try this out now.

User Gravatar

ddd-h

Posted Feb 13 2014 7:25 UTC

Yes functions.php of your theme, or the main php file of your plugin.

User Gravatar

adrian7

Posted Feb 13 2014 9:23 UTC

I've not had chance to check this out yet but I will ASAP.

@adrian7 am I copying both the first code you wrote and the second? Or just the second?

User Gravatar

ddd-h

Posted Feb 14 2014 19:08 UTC

Both version should work. I advise to try the second (optimized) version, as it has less code it will be easier to debug (just in case).

User Gravatar

adrian7

Posted Feb 15 2014 3:37 UTC

Hi Adrian,

I tried the second script in my functions and I can't view my site, it goes blank (white screen)

I tried your first script instead of the second, this shows my site but does not appear to redirect at all.

User Gravatar

ddd-h

Posted Feb 15 2014 5:30 UTC

There's a missing "}" it should close the if condition like this:

if( $userinfo and isset( $userinfo->user_url ) ){
    return $logouturl . '&amp;redirect_to=' . urlencode( $userinfo->user_url );
} 
User Gravatar

fulippo

Posted Feb 15 2014 5:51 UTC

I really must be missing something here, this has fixed the blank screen issue, but the redirect still doesn't appear to be working.

User Gravatar

ddd-h

Posted Feb 15 2014 6:16 UTC

Try modifying the function code function usersite_logout_redirect_url( $logouturl, $redirect ) { as the hook call says there should be 2 parameters.

Also add a die('HO! Breakpoint!') inside the function to check if it ever gets called.

User Gravatar

adrian7

Posted Feb 15 2014 6:24 UTC

I think adrian may have meant to add an else statement in there or forgot to close the if statement bracket, either way, you need to add permission to redirect to another site as well and you would have ran into that problem later, so the code below should work for you.

/**
 * Applies the URL saved saved in user profile fields to the redirect url upon logout
*/
function usersite_logout_redirect_url( $logouturl, $redirect ) {

    $userinfo = get_currentuserinfo();

    if( $userinfo and isset( $userinfo->user_url ) ){ 
        //redirect to the custom set users url
        return $logouturl . '&amp;redirect_to=' . urlencode( $userinfo->user_url );
    } else {
       //redirect to homepage
       return $logouturl . '&amp;redirect_to=' . urlencode( get_option( 'siteurl' ) );
    }
}
add_filter('logout_url', 'usersite_logout_redirect_url', 10, 2);

So if you're redirecting to a site other than your own, you need to add this to your functions.php file:

add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
    $content[] = 'blog.example.com';
    $content[] = 'codex.example.com';
    // wrong: $content[] = 'http://codex.example.com';
    return $content;
}

Logout filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/logout_url

Required if redirecting to something other than your site: http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts

User Gravatar

tripflex

Posted Feb 15 2014 21:54 UTC

Just thought about this too after I posted, you will probably have to white list every persons website as well, which can probably be done by using a hook when profiles are updated, or you could even redirect to an internal PHP page, like logout-redirect.php?page=customersite.com and just use PHP to redirect (but you would need to sanitize the URL first to prevent injection of code).

Not sure if you are going to or what your plan is, but just a thought, let us know how it goes and what you decide would work best.

User Gravatar

tripflex

Posted Feb 15 2014 22:09 UTC

@tripflex thanks for clarifying.

Yes adding all users sites to allowed_redirect_hosts is not feasible if you have 25+ users.

Another idea is to add a piece of Javascript on the WP login/logout page to redirect to the user's site, and avoid doing 2 redirects with PHP. But PHP redirect should still work.

User Gravatar

adrian7

Posted Feb 16 2014 2:06 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions