admin管理员组文章数量:1410705
I have a Wordpress page with page_id = 19366. I would like only logged in user to view this page. If the user isn't logged in, send them to the login page, then redirect them back to the page with id 19366.
After going through about a dozen different potential solutions from search, the closest solution was the one below. Unfortunately, it only redirects to the login page but doesn't redirect again to the referred page (id 19366) after the user is logged in. If the user is logged in, the page displays fine. I've also tried a second solution, but that didn't work either.
// 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 );
I have a Wordpress page with page_id = 19366. I would like only logged in user to view this page. If the user isn't logged in, send them to the login page, then redirect them back to the page with id 19366.
After going through about a dozen different potential solutions from search, the closest solution was the one below. Unfortunately, it only redirects to the login page but doesn't redirect again to the referred page (id 19366) after the user is logged in. If the user is logged in, the page displays fine. I've also tried a second solution, but that didn't work either.
// 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 );
Share
Improve this question
asked Oct 30, 2019 at 10:31
ThomasThomas
1031 bronze badge
1 Answer
Reset to default 0You can pass wp_login_url()
a URL to redirect to upon login.
https://codex.wordpress/Function_Reference/wp_login_url
If you used the 1st Solution, right before your exit;
, you have the wp_redirect()
call. You can replace that line with the following
wp_redirect( esc_url( wp_login_url(get_permalink($page_id)) ), 307 );
and I would not use your second solution.
Hope that helps!!
本文标签:
版权声明:本文标题:functions - Allow logged in user to view a Page, else send to login screen and then redirect back to Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745039656a2639002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论