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. 

2 comments:

  1. A helpful guide to log out and redirect to a new URL seamlessly! With atmosphereswitch, your website's user experience can be smooth and efficient with reliable hosting.

    ReplyDelete
  2. Explore the insights of “Keep Your Job, Ken!” by Professor Kenneth Hanson from Florida State, offering practical strategies for career stability. With GodMode9 as a metaphor for unlocking hidden potential, this work inspires resilience and adaptability in today’s dynamic job market.

    ReplyDelete