Pages

Thursday, November 29, 2012

Logout and Redirect to a New URL

Since Wordpress 2.7, a new template tag has been introduced using which one could redirect a logged-in user to another URL. Why would one need such a function? Well, people want logged out users to still hang-around their site to check something that might interest them.

This is how you can use this template tag:

Step 1: Add this code in your functions.php


add_filter('allowed_redirect_hosts','logout_redirect');
function logout_redirect($link)
{
    $link[] = 'example.com';
    return $link;
}
Step 2: Add this code anywhere on your site, for example in a sidebar.php or header.php or footer.php:

<a href="<?php echo wp_logout_url( 'http://example.com' ); ?>" title="Logout">Logout</a>
That's it! You are done. Wait, I just forgot to caution you that the Step 2 has PHP in it. So it won't work if you are simply adding it to a HTML text widget in sidebar or if you are adding it to a page or post. You should necessarily add this to a php file, ideally sidebar.php would do.

In case you insist that you want to use it in a sidebar widget, then better install a plugin which allows PHP in sidebar widgets. 

No comments:

Post a Comment