问题:登录后,Wp重定向到Wp admin或home(front page.php),而不是返回到当前页面,我希望用户返回到他/她来自的地方好的,这就是我得到的:
我有一个带有引导选项卡的自定义页面(page-about.php)(我只在出现这些问题的情况下才会提及)。
这些选项卡是使用存储在模板部件中的部件构建的,当然还有自定义查询。
其中一些选项卡包含不应公开访问的内容。为了对未登录的用户隐藏它,我只需执行以下操作:
<!-- template-parts/board.php -->
<div class="inner-wrapper">
<?php
global $post;
// Calling the query
$firstPage = new WP_Query(array(
\'post_type\' => \'page\',
\'pagename\' => \'board\',
\'page_id\' => 317
));
// Checking if user is logged in
if (is_user_logged_in()) {
while ($firstPage->have_posts()) : $firstPage->the_post();
?>
<!-- A bunch of html in between here -->
<?php endwhile;
wp_reset_postdata();
// If user is not logged in, the text below is shown asking user to log in.
} else { ?>
<p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url(wp_login_url()); ?>"><?php _e(\'log in\') ?></a> now?</p>
<?php } ?>
</div>
关于在获取
wp_login_url(), 我也试过使用
<?php
// Saving the current url in a var
// Hovering the link gives me: https://localhost:3000/wp-login.php?redirect_to=https%3A%2F%2Fmysite.local%2Fabout
$current_url = home_url( add_query_arg( [], $GLOBALS[\'wp\']->request ) ); ?>
<p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url(wp_login_url(site_url(add_query_arg(array(), $wp->request)))); ?>" alt="<?php esc_attr_e(\'login\', \'textdomain\'); ?>"><?php _e(\'log in\', \'textdomain\'); ?></a> now?</p>
然而,我仍然被重定向到wp管理员。
我了解到,这可能不是处理重定向的最佳方式,因为Wordpress倾向于如何以及何时调用受影响的函数,所以我也尝试将这些函数添加到函数中。php(显然不是同时):
<?php
// #1. found on developer.wordpress.org -> login_redirect
function my_login_redirect($redirect_to, $request, $user)
{
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for admins
if (in_array(\'administrator\', $user->roles)) {
// redirect them to the default place
return $redirect_to;
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
// #2. A modified version of the above found here on stackexchange or stackoverflow
add_filter(\'login_redirect\', \'my_login_redirect\', 10, 3);
function my_login_redirect($redirect_to, $requested_redirect_to, $user)
{
if (isset($user->roles) && is_array($user->roles)) {
if (in_array(\'subscriber\', $user->roles)) {
if ($requested_redirect_to && admin_url() != $requested_redirect_to) {
$redirect_to = $requested_redirect_to;
} else {
$redirect_to = home_url();
}
}
}
return $redirect_to;
}
add_filter(\'login_redirect\', \'my_login_redirect\', 10, 3);
?>
所以
my main question is: 如何强制Wordpress将用户重定向到与使用
custom page template 和自定义查询?以上这些似乎都不适用。
谢谢你,对此表示歉意!