I have a function that redirects users to the login page (home) if they’re trying to access any other page without being logged in, here’s how it works:
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_redirect( get_option('home') );
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
Really simple and works fine, the problem is that is that I need to redirect them to the url they were trying to go to after they successfully log in, exactly like the WordPress backend works.
Is there a way to do this? Thanks in advance!
You can do that easily. You just need to specify a redirection parameter.
If you are using a login link on the homepage to go to the login page, then @sisir’s solution is correct.
<?php echo wp_login_url( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ); ?>
If you are using a custom form on the frontpage, then inside the
<form>
, make sure you fill in a hidden field with the url to redirectAnd if you are using
wp_login_form()
to generate the form, then fill in a parameter – http://codex.wordpress.org/Function_Reference/wp_login_formChange other parameters as per what you have or need.
Try passing
the_permalink()
as the$redirect
argument:EDIT:
Sorry, misunderstood your question originally. Try this:
Note also: proper usage of
wp_redirect()
generally requires addingexit;
, which I have added to my second example.Thanks all, I kind of used a bit of what everyone recommended so in the end my code looks like this:
And on my login form (I’m hardcoding my login form in my aplication thanks @Ashfame for letting me know about wp_login_form I had no idea it existed) I added this when user credentials are fine and they’re ready to login:
Thanks a lot for your help, I voted up everyone!
this is my code that i use people to direct to wp login page. Then when logged in they returned back to where they were. But it is not home page but the wordpress login page where i setup custom login.
You might wanna research with. Typically you will get the current url of a user by
$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]
I know this is super late, but I made a post about exactly how to do this if any future people find this and need it:
http://www.ryanprejean.com/force-login-with-redirect-and-exceptions/
The
login_redirect
filter hook is a more complete and effective solution here. This way, you can offer different redirect paths for different levels of users, or maintain the redirect URL in the case of an error when logging in (i.e. Incorrect Password).It has not worked any of your answers but only adding a little thing, it worked!
Here my code:
Only I added
/wp-login.php
in comparison to @Matt’s response, but for me has been the key. Hope that helps! 🙂**EDIT:
I detected an ERROR when you are FORCE wordpress to navegate in HTTPS. This method doesn’t work because the redirection is in HTTP. For fix the problem I changed the function. This is the result:
I check the protocol and then I deleted ‘
esc_url
‘ and added the correct protocol:$protocol://
. Also I changed the""
.I’m based with this page.