Wordpress redirects to link page after login

Note. I have already created the same question in Wordpress StackExchange , but I have not received any answers! Sorry for that!

I do not use any custom login plugins or any custom code. Some of my pages got this bit of code at the very beginning.

<?php if(!is_user_logged_in()) wp_redirect('/login/'); ?> 

Thus, this does not allow users to view the page when you are not logged in. I have these pages with this bit of code:

 /wp-content/my-theme/my-account/ /wp-content/my-theme/my-account/world.php /wp-content/my-theme/my-account/subscription.php /wp-content/my-theme/my-dashboard.php /wp-content/my-theme/my-files.php 

Now, when the user goes to any of the above pages without logging in, he is redirected to the login page, and when the user logs in, he places them on the my-account/ page.

I want to change the current script so that the user redirects to the link page where he came from. I tried the following things that never worked.

Using HTTP_REFERRER

In the login/ form login/ I put this bit of code:

 <input type="hidden" name="redirect" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" /> 

Hacking functions.php

In functions.php I put this bit of code:

 if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) { add_filter('login_redirect', 'my_login_redirect', 10, 3); function my_login_redirect() { $location = $_SERVER['HTTP_REFERER']; wp_safe_redirect($location); exit(); } } 

Literature:


I also tried these and failed:

Nothing succeeded. I am pleased to provide additional information if necessary. Thank you in advance. :)


My work is still ...

I changed the code like this:

 <?php if(!is_user_logged_in()) wp_redirect('/login/?redirect_to=' . $_SERVER["REQUEST_URI"]); ?> 

This makes the login page as follows:

 /login.php?redirect_to=/my-account/subscription.php 

That would be enough for authentication and redirection. But I need to find the bit where the real redirect occurs, and I want to redirect it using the redirect_to parameter!

+6
source share
1 answer

The login process is handled through AJAX. The function sending the values ​​does not consider redirect_to . The following code in the JS file is always redirected to / my -account. So, now when the AJAX function returns, you can get the redirect_to value of the hidden field and then redirect the user there.

 window.location = "/my-account/"; 
+1
source

Source: https://habr.com/ru/post/978788/


All Articles