WordPress: Redirecting Login/out

025th Aug 2010Wordpress, , , , ,

Wordpress LogoTwo very useful filters for plugin developers are “logout_redirect” and “login_redirect”.  Using these filters, you can change what pages a user will be sent to when they either log in or out of your WordPress blog.  Let’s look at some examples:

The first thing we’ll do is set up the filter hooks in our plugin.  This is fairly simple and requires only that we give the name of the callback method which WordPress should execute when the specified action occurs.

1
2
add_filter('logout_redirect','my_logout_redirect_method');
add_filter('login_redirect','my_login_redirect_method');

As you can see, we’re adding a filter that tells WordPress that we’re going to execute the routine “my_logout_redirect_method” when a user logs out, and “my_login_redirect_method” when a user logs in.  Simple, no?  Let’s look at the methods.

1
2
3
function my_logout_redirect() {
return '/';
}
1
2
3
function my_login_redirect() {
return '/';
}

What happens here is that, when a user logs in or out, they’re automatically redirected to the root of your domain. For example, http://www.anthonygcyphers.com/.

No Comments Comments Feed

Add a Comment

Before you post, please prove you are sentient.

what is 6 + 2?