zel zel - 4 months ago 11
PHP Question

Redirect to page after login, when you came from specific page in wordpress

I need a very simple thing in wordpres
First one is:
I have a specific page called profile - when user can edit his/her info and change password.
If someone is not logged-in - he should be redirected to loginpage.
So, it would be something on this page

if ( is_user_logged_in() ) {
} else {
wp_redirect( 'http://www.mysite.com/wp-login.php' ); exit; }


That's part ok, but if someone login (push the button 'login') i'd like him to go back to the previous site. So I came up with this

wp_redirect( 'http://www.mysite.com/wp-login.php?=profile' ); exit; }


And then, php would check if there's ?=profile in the address and with proper function redirect to /profile after login.

Could please someone help me with the second chunck of code? :) Thanks!

Dev Dev
Answer

If you want custom redirection, you can use like this

$returnPath = get_settings('siteurl') . '/login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI']);

<a href="<?php echo $returnPath;?>">Login  link</a>
Comments