我有一个Wordpress页面,page\\u id=19366。我只希望登录用户查看此页面。如果用户未登录,请将其发送到登录页面,然后将其重定向回id为19366的页面。
在研究了十几种不同的搜索潜在解决方案后,最接近的解决方案是下面的一种。不幸的是,它只重定向到登录页面,但在用户登录后不会再次重定向到引用页面(id 19366)。如果用户已登录,则页面显示良好。我还尝试了第二种解决方案,但也没有成功。
// Solution 1: Restrict access to specific pages
add_action( \'template_redirect\', function() {
// Get global post
global $post;
$where_from = $_SERVER["HTTP_REFERER"];
// Prevent access to page with ID of 19366 and all children of this page
$page_id = 19366;
if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
// Set redirect to true by default
$redirect = true;
// If logged in do not redirect
// You can/should place additional checks here based on user roles or user meta
if ( is_user_logged_in() ) {
$redirect = false;
}
// Redirect people without access to login page
if ( $redirect ) {
function my_login_redirect( $redirect_to, $request, $user ) {
$redirect_to = $where_from;
return $redirect_to;
}
add_filter( \'login_redirect\', \'my_login_redirect\', 10, 3 );
wp_redirect( esc_url( wp_login_url() ), 307 );
exit;
}
}
} );
<小时>
// Solution 2
function my_login_redirect( $redirect_to, $request, $user ) {
// Get global post
global $post;
$where_from = $_SERVER["HTTP_REFERER"];
// Prevent access to page with ID of 2 and all children of this page
$page_id = 19366; // Happy Volleydays Tournament Poll. Page ID = 19366
//if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
if ( is_page( $page_id ) && ! is_user_logged_in() ) {
return $request;
}
return $redirect_to;
}
add_filter( \'login_redirect\', \'my_login_redirect\', 10, 3 );