Wordpress page show only to logged in users?

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,147
Reaction score
10,489
I have a page on a website, and I want it to show only for logged in users. If a user is not logged in, they should be redirected to the login page.

And optionally as a bonus, if they do get to the login page through this process, after they login, they should be redirected to the page they initially tried to visit.

Page:

https://mysite.com/super-cool
I tried the google, but not getting much. There are these two threads:

https://stackoverflow.com/questions/61519270/contact-form-7-only-show-for-logged-in-usershttps://stackoverflow.com/questions/35576100/how-to-only-show-logged-in-users-posts-to-user
The first one he never got the answer about where to put the code. The second one is a bit different than what I need.

I'd rather not use a plugin for this, if at all possible.

Can anyone help? @TomTheCat @BlogPro
 
You can do that with easy code

Add this on your functions.php

Code:
add_shortcode('verify', 'shortcode_verify');

function shortcode_verify() {
    if (!is_user_logged_in()) {
        auth_redirect();
    }
}

then add this short code on top of page to enable login

[verify]
 
You can do that with easy code

Add this on your functions.php

Code:
add_shortcode('verify', 'shortcode_verify');

function shortcode_verify() {
    if (!is_user_logged_in()) {
        auth_redirect();
    }
}

then add this short code on top of page to enable login

[verify]

Oh killer, yes, that worked, thank you.

But hold on a second. My website's login page is not the default Wordpress login page.

How can I set the redirect to take a person to a specific URL?

This?

PHP:
add_shortcode('verify', 'shortcode_verify');

function shortcode_verify() {
    if (!is_user_logged_in()) {
        auth_redirect(https://mysite.com/page);
    }
}
 
you can also create separate function for that but this code help

Code:
  wp_redirect( home_url( '/signup/' ) );
 
Last edited:
you can create separate function for that

Code:

Okay I did some googling and thinking :D and I got this:

Code:
add_shortcode('verify', 'shortcode_verify');

function shortcode_verify() {
    if (!is_user_logged_in()) {
        wp_redirect( 'https://site.com/sign-in/', 302 );
    }
}

It works!

Please just let me know if maybe this is not good, and if there is a better way.

Like this solution is a 302, which is a not permanent redirect as far as I know. So I don't know, just wondering if this is okay.?
 
Okay. But why use a plugin when I already have working code?

And the code is like 5 lines of code, not 3000 lines of code. Like, no security risk.

Ey?
yes for that you need to use child theme and do modification on child theme functions.php. If you update original theme your modification can lost.
 
This plugin will save you a lot of time and efforts: https://nl.wordpress.org/plugins/content-control/

Man, I really don't understand you guys.

I've always read that you should reduce the amount of plugins you use on a site. Plugins can be a security risk. Plugins can slow your site down. Plugins can be incompatible with your site and cause weird problems, etc etc etc.

And then I ask to do something that 5 lines of code can fix. One, two, three, four, five lines of code. And I get hit with google results and replies here with, "oh yeah you should use a plugin for that."

I don't understand... :confused::oops:o_O:(
 
Man, I really don't understand you guys.

I've always read that you should reduce the amount of plugins you use on a site. Plugins can be a security risk. Plugins can slow your site down. Plugins can be incompatible with your site and cause weird problems, etc etc etc.

And then I ask to do something that 5 lines of code can fix. One, two, three, four, five lines of code. And I get hit with google results and replies here with, "oh yeah you should use a plugin for that."

I don't understand... :confused::oops:o_O:(
Forget the plugin and shit man!

Stick to the few lines of code that's already working for you, like a charm. You won't go wrong. I mean there is absolutely no need to plugin trash into your site when you don't have to.
 
Forget the plugin and shit man!

Stick to the few lines of code that's already working for you, like a charm. You won't go wrong. I mean there is absolutely no need to plugin trash into your site when you don't have to.

Kisses and hugs for you sir haha :D

Agreed
 
Don't use a short code or a plugin for such basic functionality. Shortcodes are extremely inefficient and plugins even worse (because they add much more functionality that what you need in such easy situation).

My opinion: create your own features plugin and add the wp_redirect

First to do the redirect to login:

PHP:
add_action( 'admin_init', 'login_if_user_not_logged_in' );
 
function login_if_user_not_logged_in() {
 if ( !is_user_logged_in() && is_page('page_ID ') ) {
     wp_redirect( 'your_login_page ');
  }
}

Note that you have to hardcode the page_ID, since it is your plugin, you can edit it whenever you want.

You need to modify the login_redirect filter if you want to do a login back redirect to the original post (or any post after login)

PHP:
function redirect_to_posts_after_login($redirect_to) {
    $redirect_to_post = filter_input(INPUT_POST, 'redirect_to_post_url');

    if (!$redirect_to_post === "") {
        wp_redirect(esc_url($redirect_to_post));
    } else {
        return $redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 1);
 
Don't use a short code or a plugin for such basic functionality. Shortcodes are extremely inefficient and plugins even worse (because they add much more functionality that what you need in such easy situation).

My opinion: create your own features plugin and add the wp_redirect

First to do the redirect to login:

PHP:
add_action( 'admin_init', 'login_if_user_not_logged_in' );
 
function login_if_user_not_logged_in() {
 if ( !is_user_logged_in() && is_page('page_ID ') ) {
     wp_redirect( 'your_login_page ');
  }
}

Note that you have to hardcode the page_ID, since it is your plugin, you can edit it whenever you want.

You need to modify the login_redirect filter if you want to do a login back redirect to the original post (or any post after login)

PHP:
function redirect_to_posts_after_login($redirect_to) {
    $redirect_to_post = filter_input(INPUT_POST, 'redirect_to_post_url');

    if (!$redirect_to_post === "") {
        wp_redirect(esc_url($redirect_to_post));
    } else {
        return $redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 1);

I agree about the plugin. Not sure about shortcodes, being inefficient, but I'll take your word for it.

But this doesn't work:

Code:
add_action( 'admin_init', 'login_if_user_not_logged_in' );
 
function login_if_user_not_logged_in() {
 if ( !is_user_logged_in() && is_page('2862 ') ) {
     wp_redirect( 'https://site.com/sign-in/ ');
  }
}

I tried both logged in and not logged in, and it doesn't work.
 
Check that space after 2862

I checked both spaces, doesn't work:

PHP:
add_action( 'admin_init', 'login_if_user_not_logged_in' );
 
function login_if_user_not_logged_in() {
 if ( !is_user_logged_in() && is_page('2862') ) {
     wp_redirect( 'https://site.com/sign-in/');
  }
}

Tried logged in and not logged in. Cleared browser cache. Cleared site cache. Tried different browsers.

Doesn't work :/
 
And I'm pretty sure I have the correct Page ID:

ididid.PNG
 
I tried this too, doesn't do anything:

PHP:
add_action( 'admin_init', 'login_if_user_not_logged_in' );
 
function login_if_user_not_logged_in() {
 if ( !is_user_logged_in() && is_page('https://site.com/asd-sad/') ) {
     wp_redirect( 'https://site.com/sign-in/');
  }
}
 
Back
Top