Note: I already created the same question in Wordpress StackExchange, but I didn't receive any responses! Sorry about that!
我没有使用任何自定义登录插件或任何定制代码。我的一些页面一开始就有这段代码。
<?php
if(!is_user_logged_in())
wp_redirect('/login/');
?>
所以,这不允许用户在未登录时查看页面。我有这些页面带有这段代码:
/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
现在,当用户在没有登录的情况下进入上述任何页面时,它会重定向到登录页面,当用户登录时,它会将他们登陆到 my-account/
页面。
我想改变当前的场景,让用户重定向到他来自的引用页面。我尝试了以下方法,但从未奏效。
使用HTTP_REFERRER
在 login/
表单中,我放置了这段代码:
<input type="hidden" name="redirect" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
破解functions.php
在 functions.php
中,我放置了这段代码:
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();
}
}
引用资料:
- Redirect wordpress back to referring page after login
- WordPress Tip: Redirect to Previous Page After Login
我也试过这些但失败了:
- Redirecting users to referrer page after logging in using custom login form
- Redirect user to original url after login?
什么都没有解决。如果需要,我很乐意提供更多详细信息。提前致谢。 :)
到目前为止我的工作...
我这样修改了代码:
<?php
if(!is_user_logged_in())
wp_redirect('/login/?redirect_to=' . $_SERVER["REQUEST_URI"]);
?>
以这种方式呈现登录页面:
/login.php?redirect_to=/my-account/subscription.php
这足以让我进行身份验证和重定向。但我需要找到真正重定向发生的位,我想使用 redirect_to
参数重定向它!
最佳答案
登录过程是通过 AJAX 处理的。发布值的函数不考虑 redirect_to
。 JS 文件中的以下代码始终重定向到/my-account。所以现在当 AJAX 函数返回时,您可以获得 redirect_to
隐藏字段的值,然后将用户重定向到那里。
window.location = "/my-account/";
关于php - 登录后 Wordpress 重定向到引用页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171650/