functions - Allow logged in user to view a Page, else send to login screen and then redirect back to Page

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

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039656a4607735.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信