
“Peter’s Login Redirect” is a plugin for WordPress that allows you to customize where users are redirected to after logging in. The plugin can be reviewed/downloaded here:
http://wordpress.org/extend/plugins/peters-login-redirect/
The problem
If you’re using a log-in widget in your sidebar it’s likely that when the user logs in/out you want to redirect them to the same page. This is mainly for the user’s convenience. As an example, the bbPress 2.0 plugin for WordPress offers a sidebar widget for users to log in/out. We want the log-in widget to redirect to the same page however “Peter’s Login Redirect” overrides this process. We need to override/restrict the plugin’s override for any pages containing the log-in widget but preserve the plugin redirect settings for /wp-login.php.
The solution
Fortunately, the plugin author provides an example of how to override the plugin redirect settings by adding a few lines of code to your functions.php file. After 2.5 obsessive days, I’m happy to present the following solution:
// Plugin by http://wordpress.org/extend/plugins/peters-login-redirect/
// Solution by http://www.dominornovus.com
function login_widget_redirect( $empty, $redirect_to, $requested_redirect_to, $user )
{
// If the referring page contains "/forums/"...
if (stripos($_SERVER['HTTP_REFERER'],'/forums/') !== false)
{
$referringpage = $_SERVER[HTTP_REFERER];
return $referringpage;
}
//Otherwise, check the referring page contains "/blog/"...
else if (stripos($_SERVER['HTTP_REFERER'],'/blog/') !== false)
{
$referringpage = $_SERVER[HTTP_REFERER];
return $referringpage;
}
// Otherwise, default to the plugin's redirect settings...
else
{
return false;
}
}
add_filter( 'rul_before_user', 'login_widget_redirect', 10, 4 );
Just copy and paste the above into your functions.php file. The “/forums/” and “/blog/” values represent the slugs as they appear in your page URL. Obviously, make sure these values match whatever slugs you use. If you want to target just the one slug, delete the “else if” part of the condition. If you’re targeting more than two slugs, simply add an additional “else if” to your condition.
Explanation
It took me the longest time to figure out that the WordPress (or bbPress) conditional tags weren’t meant to be working in my function. Conditional tags include useful functions such as is_home(), is_page() or is_bbpress() etc. Assuming the worst, I tried absolutely everything else until stumbling across this gem:
Warning: You can only use conditional query tags after the init action hook in WordPress. For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function. [Source]
This little confirmation assured me that there are indeed instances where the conditional tags will not work. After reviewing the author’s example, I noticed he wasn’t relying on conditional tags either.
After ruling out the use of conditional tags with my function, I used a PHP function that checks if the current URL contains a specific string. In my case, my sidebar log-in widget appears on my forum and blog pages. The intention was to check the current URL for specific slugs such as “forum” and “blog”. For whatever reason, the current URL for sidebar log-in widget returns as http://www.somewebsite.com/wp-login.php. This resulted in my condition returning as false and prompted the plugin to apply the regular redirect settings.
After some thought, I targeted the referring URL rather than the current URL. The referring URL seems to be whatever page the user is logging in from. i.e. wp-login.php or the sidebar log-in widget. Lo and behold, this worked. Now, if the user logs in from a URL containing ”/forums/” or “/blog/”, the user will “stay” on the same page (as far as the user is concerned – in actuality, it’s a same-page redirect though it’s not perceptible to the user). Furthermore, if the user logs in from any page other than a forum or blog page, the plugin settings are allowed to do as they please.
Your thoughts
Can you improve this code? Let me know. Hope it helps!




