0

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 );
0

You can pass wp_login_url() a URL to redirect to upon login. https://codex.wordpress.org/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!!

| improve this answer | |
  • Thank you for sharing Chris. How would your code work with either of my two original solutions? – Thomas Thai Oct 31 '19 at 16:21
  • @ThomasThai, I've updated my answer to hopefully give you some direction. I would use your 1st solution. – ChristopherJones Oct 31 '19 at 17:11
  • @ThomasThai, if the answers works for you, would you mind accepting it? Thanks!! – ChristopherJones Jan 10 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.