I’m working on a WordPress site which has various features that are only available to registered users. I need to prompt the users to register/login at various places, and after they do I want to return them to the original page they were on.
I know how to do this manually, but my suspicion is that there is a way to take advantage of built-in WordPress functions, so I’d rather not “reinvent the wheel.” I’ve searched for solutions but I’ve only found strategies that involve a hard-coded, static return page after registration. I DON’T want to do this. I’m looking for a way to keep track of the referring page, and return to it after registration. Thanks in advance.
EDIT1: I’ve located what I thought I was looking for, which is the filter hook login_redirect. It appears to be designed for exactly what I want to do, but I don’t seem to be able to get it to work.
The implementation should be pretty straight-forward, so I’m not sure what I could be doing wrong. Here’s the code in my theme’s functions.php:
function redirect_to_function($redirect_to, $request, $user)
{
return 'http://www.mywebsite.com/redirect_page/';
}
add_filter( 'login_redirect', 'redirect_to_function', 10, 3 );
EDIT2:
I think I solved the mystery as to why the redirect_to filter isn’t working, as well as the solution using the hidden field named “redirect_to” (see comments below). I’m using the s2Member plugin to manage membership levels, and I suspect that it is overriding the built-in wordpress functionality.
My problem was related to a specific plugin I was using that was over-riding my hooks. See my edits above.