php - Redirect a user to the page they were trying to access after login in Wordpress -
i have page restricted logged in users (say it's called '/authed-page'). when non-authenticated user visits page prompted click button login , taken login url: '/login'
now after login want them redirected page had been trying visit, '/authed-url', can't figure out how this!
things i've tried:
1) declaring function redirect_to_authed_url in functions.php file this
function redirect_to_authed_url( $redirect_to, $request, $user ) { return "/post-a-job"; }
then in template authed-url.php when detect user isn't logged in this:
<?php add_filter( 'login_redirect', 'redirect_to_authed_url', 10, 3 ); ?>
however doesn't work think because user presses button generates http request sends them '/login' url means redirect filter lost.
2) setting cookie when visits '/authed-url' , not logged in called 'redirect_url'. declare in theme's functions.php file
function redirect_request( $redirect_to, $request, $user ) { if (isset($_cookie['login_redirect']) && strpos($_cookie['login_redirect'], 'http') === false) { $redirect_url = $_cookie['login_redirect']; unset $_cookie['login_redirect']; return $redirect_url; } else { return('/'); } }
this doesn't seem work because can't figure out set cookie. keep getting warning:
warning: cannot modify header information - headers sent
i feel must missing obvious. should pretty common requirement no? ideas?
you able append url encoded redirect_to variable login url redirect after login.
for example:
http://www.example.com/wp-login.php?redirect_to=http%3a%2f%2fwww.example.com%2fauthed-url
that url redirect /authed-url after login.
Comments
Post a Comment