Wordpress page show only to logged in users?

Tried this too:

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

Doesn't work.
 
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);

Wait a minute, I'm putting the code in functions.php

When you say I need to create a plugin, do you mean I have to create a separate php file and put the code in it?
 
Doesn't work :/
I was checking this example, which on the paper looks pretty basic:
https://developer.wordpress.org/reference/functions/is_user_logged_in/#comment-4904
I've tried also it doesn't work for me either.
This is when I start hitting the table with fking WordPress and switch back to Laravel :p

On functions.php should work either, it doesnt matter if you do it on a separate plugin as long as the functions.php is on a child theme. In fact I'm testing this with Twenty Twenty-One on my test site which is freaking me out.
 
I was checking this example, which on the paper looks pretty basic:
https://developer.wordpress.org/reference/functions/is_user_logged_in/#comment-4904
I've tried also it doesn't work for me either.
This is when I start hitting the table with fking WordPress and switch back to Laravel :p

On functions.php should work either, it doesnt matter if you do it on a separate plugin as long as the functions.php is on a child theme. In fact I'm testing this with Twenty Twenty-One on my test site which is freaking me out.

I don't use child themes bro.

I have 1 website. 1 set of files. I set of edits. I never got into child themes. Wordpress is enough of a pain in the ass without doubling the pain in the ass with a child theme :p

Okay, so seeing as what you gave doesn't work, I'm gonna have to go back to the shortcode method. I hope it's not too extremely inefficient :( :p

---

But now that you said that shortcodes are extremely inefficient, you got me worried. This page I'm dealing with here is very important for my site...
 
But now that you said that shortcodes are extremely inefficient, you got me worried. This page I'm dealing with here is very important for my site...
Solved

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(199) ) {
        wp_redirect('https://mysite.com/wp-login.php', 301);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

Issues:

1. admin_init was too early for the redirect, if you are doing this on functions.php then must be executed a little bit later (on template_redirect hook). This is 99.9999999% of the times my main pain in the ass. I have serious troubles identifying the best hook for many wordpress functions.
2. I was testing with a POST not a PAGE therefore is_single is required. In case it is a Custom Post Type it will also be different. In case it is a post then the function is is_single(ID) otherwise is_post(ID).

About the child theme, I recommend you to set up a child theme, because if you update your theme version, all the things you put in your functions.php will be wiped out after the update.
 
Solved

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(199) ) {
        wp_redirect('https://mysite.com/wp-login.php', 301);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

Issues:

1. admin_init was too early for the redirect, if you are doing this on functions.php then must be executed a little bit later (on template_redirect hook). This is 99.9999999% of the times my main pain in the ass. I have serious troubles identifying the best hook for many wordpress functions.
2. I was testing with a POST not a PAGE therefore is_single is required. In case it is a Custom Post Type it will also be different. In case it is a post then the function is is_single(ID) otherwise is_post(ID).

About the child theme, I recommend you to set up a child theme, because if you update your theme version, all the things you put in your functions.php will be wiped out after the update.

Okay, I'll give it a shot. Let's see what happens...

img404.jpg
 
Solved

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(199) ) {
        wp_redirect('https://mysite.com/wp-login.php', 301);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

Issues:

1. admin_init was too early for the redirect, if you are doing this on functions.php then must be executed a little bit later (on template_redirect hook). This is 99.9999999% of the times my main pain in the ass. I have serious troubles identifying the best hook for many wordpress functions.
2. I was testing with a POST not a PAGE therefore is_single is required. In case it is a Custom Post Type it will also be different. In case it is a post then the function is is_single(ID) otherwise is_post(ID).

About the child theme, I recommend you to set up a child theme, because if you update your theme version, all the things you put in your functions.php will be wiped out after the update.

Nope, it didn't work.

Tried these:

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(199) ) {
        wp_redirect('https://mysite.com/wp-login.php', 301);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(2862) ) {
        wp_redirect('https://mysite.com/wp-login.php', 301);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

The page in question is just a regular Wordpress page. Nothing special about it.

And about the updates, that's not a problem, I am a crusader against updates. I never update anything, unless I must :D

Tried this too, because you never know, but doesn't work:

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single(2862) ) {
        wp_redirect('https://site.com/wp-login.php', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );
 
And about the updates, that's not a problem, I am a crusader against updates. I never update anything, unless I must :D
You should be upgrading frequently. Problem with Wordpress is a exploit nest. There are issues emerging constantly so if you don't upgrade you may end with your site hacked the sooner than the later. In fact I have 100+ being updated unattended and daily with with my MainWP control panel.

Anyway, if its a page then you should be using is_page(2862) not is_single.

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page(2862) ) {
        wp_redirect('https://site.com/wp-login.php', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );
 
You should be upgrading frequently. Problem with Wordpress is a exploit nest. There are issues emerging constantly so if you don't upgrade you may end with your site hacked the sooner than the later. In fact I have 100+ being updated unattended and daily with with my MainWP control panel.

Anyway, if its a page then you should be using is_page(2862) not is_single.

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page(2862) ) {
        wp_redirect('https://site.com/wp-login.php', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

No no, I shall not be updating frequently. I have this HTML site, hasn't been really updated in oh 8 years. Works just as well as it did on day 1. And my wordpress sites, yeah I had a small hack a few months ago, but those sites also ran for like 5+ years with no updates, and never had any problems.

Pro: 5 years of peace of mind
Con: 1 small hack

Yeah, seems like a great deal to me :D

And even a massive hack, who gives a shit, I'll remove the entire site and reupload a backup, and take it from there.

I just can't live my life with the constant wordpress updates. I need my site to work. I need it to work every day. I need it to work all the time. Every day. No matter happens. War in Ukraine, price of tea in China, me in a coma, I need it to work. Updates simply do not allow for such peace of mind.

---

Okay, I got it working finally! :)

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page( 'add-funds' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page( '2862' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

Both of these work. They just needed the quotes and the spaces. Go figure

I got the idea here (scroll down, see how they did it - quotes+spaces) - https://developer.wordpress.org/reference/functions/is_page/


These do NOT work:

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single( '2862' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single( 'add-funds' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

So I think we're good bro. I think we won this one :D
 
No no, I shall not be updating frequently. I have this HTML site, hasn't been really updated in oh 8 years. Works just as well as it did on day 1. And my wordpress sites, yeah I had a small hack a few months ago, but those sites also ran for like 5+ years with no updates, and never had any problems.

Pro: 5 years of peace of mind
Con: 1 small hack

Yeah, seems like a great deal to me :D

And even a massive hack, who gives a shit, I'll remove the entire site and reupload a backup, and take it from there.

I just can't live my life with the constant wordpress updates. I need my site to work. I need it to work every day. I need it to work all the time. Every day. No matter happens. War in Ukraine, price of tea in China, me in a coma, I need it to work. Updates simply do not allow for such peace of mind.

---

Okay, I got it working finally! :)

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page( 'add-funds' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_page( '2862' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

Both of these work. They just needed the quotes and the spaces. Go figure

I got the idea here (scroll down, see how they did it - quotes+spaces) - https://developer.wordpress.org/reference/functions/is_page/


These do NOT work:

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single( '2862' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

PHP:
function simple_redirect() {
    if ( !is_user_logged_in() && is_single( 'add-funds' ) ) {
        wp_redirect('https://digesale.com/sign-in', 302);
        exit();
   }
}
add_action( 'template_redirect', 'simple_redirect' );

So I think we're good bro. I think we won this one :D

No wait. It worked, and now it doesn't. WTF?

Edit: never mind, I had left it to is_single in the end, but it has to be is_page

We cool, we cool :)
 
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);

By the way, I was so happy we took care of the problem correctly, that I totally forgot you addressed my optional desire too :D

So the latter part of your reply, the second bit of code. I put that in functions.php, and it will do the process?

page -> redirect -> login page -> redirect -> back to page
 
By the way, I was so happy we took care of the problem correctly, that I totally forgot you addressed my optional desire too :D

So the latter part of your reply, the second bit of code. I put that in functions.php, and it will do the process?

page -> redirect -> login page -> redirect -> back to page

Theoretically it should. I have not tested but checking it from the source, it seems the right solution.
 
No no, I shall not be updating frequently. I have this HTML site, hasn't been really updated in oh 8 years. Works just as well as it did on day 1. And my wordpress sites, yeah I had a small hack a few months ago, but those sites also ran for like 5+ years with no updates, and never had any problems.
Maybe I've had too much unluck, or perhaps I manage too many sites, and this increases the odds significantly.

But you have to be warned about this. It's not just about "a backup and happy to go"

To put you in context, one of the latest exploits (for not updating one of my sites) took control of my root directory. Then they were able to verify my property and GSC, they created around 1million subpages within my site, and sent them to index the whole 1M through some kind of script

It took me 6 months to recover this site from a huge SERPs shadowban.

Furthermore, this f*****s do plenty of shadow techniques after hacked.
For example, a fake A/B in your site: when you access your site, you may see everything is right, but you are presenting to Googlebot User Agent a completely different aspect (with hundreds of links to their sites). You might have seen in this forum many people trying to sell this kind of links (although it doesn't abide the rules).

So the backup for me is one of the minor issues I take into consideration. Nowadays, I have some extra checks like e2e monitors setting Googlebot User-Agent for the test. Still, I never know how this guys will f**k one of my sites again through any of the hundreds of exploits that happen every month.

I had one site not updated from one of my clients since 2008, and not hacked at all. But this doesn't mean for me that I should not cover myself accordingly. It's true that even updates don't cover you from these hacks, but not updating is the n1 reason to have issues.
 
Theoretically it should. I have not tested but checking it from the source, it seems the right solution.

Okay, I'll give it a shot. Will let you know how it goes...

Maybe I've had too much unluck, or perhaps I manage too many sites, and this increases the odds significantly.

But you have to be warned about this. It's not just about "a backup and happy to go"

To put you in context, one of the latest exploits (for not updating one of my sites) took control of my root directory. Then they were able to verify my property and GSC, they created around 1million subpages within my site, and sent them to index the whole 1M through some kind of script

It took me 6 months to recover this site from a huge SERPs shadowban.

Furthermore, this f*****s do plenty of shadow techniques after hacked.
For example, a fake A/B in your site: when you access your site, you may see everything is right, but you are presenting to Googlebot User Agent a completely different aspect (with hundreds of links to their sites). You might have seen in this forum many people trying to sell this kind of links (although it doesn't abide the rules).

So the backup for me is one of the minor issues I take into consideration. Nowadays, I have some extra checks like e2e monitors setting Googlebot User-Agent for the test. Still, I never know how this guys will f**k one of my sites again through any of the hundreds of exploits that happen every month.

I had one site not updated from one of my clients since 2008, and not hacked at all. But this doesn't mean for me that I should not cover myself accordingly. It's true that even updates don't cover you from these hacks, but not updating is the n1 reason to have issues.

Yeah, the big hack I had a few months ago resulted in my hosting provider suspending me. I was getting tons of hits from bing on pages that don't exist. It increased my CPU usage significantly, and my host (shared hosting) suspended my account. All my sites went down, because I use the same hosting for all. Some heavy duty shit.

But anyway I cleaned it all up, and we're good now. It was a big deal when it happened, but looking back at it now it was not a big deal comparing to dealing with problems every week or two. If wordpress and plugins update every 3-5 days, and if each update can break a page, a post, a plugin, a button, a functionality, or the entire site, then we're looking at constant pains in the ass.

The good thing about me is, while I do care about SEO to an extent, I don't really give a shit about SEO. I care about being indexed, at least on google. I would like for my sites to rank as best as possible, but I'm not really gonna do too much about it or spend money on it. If I want traffic, I'll spam. Then I don't have to worry about all of that SEO insanity :)

We all make our choices. If someone knows how to deal with constant problems on their wordpress, or pays a host to manage it for them, or pays a person to manage it for them, good for them. But I like my money here, and I like my days undisturbed, and I like my sites working, like a rock on the table works (a rock on the table doesn't do anything, exactly). So I kill all updates, and wait for the time I'm forced to update, or a hack (knock on wood) :p
 
Okay, regarding the other code. This website I'm working on is new, it's been only like 3 days since it went live. I never really checked how the login redirects on the site work and where they take you.

The site has a login popup, so you can login from any page. It's not the default wordpress login page. I just tested, and when you login it will keep you on the same page where you are. But when we do our redirect, you're not logging in from the popup, you're logging in from the site's dedicated sign in page. And after you login, it takes you to your Dashboard page.

I added your code. Nothing changed.

Two questions.

1) Am I supposed to change something in the code you gave me? Maybe change the INPUT_POST to the URL where I want to redirect to 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);

2) Assuming we get it working, is this redirect going to affect every login, or is it going to affect only the login that happens after the redirect we previously implemented?
 
code is

add_shortcode('verify', 'shortcode_verify');

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

add_shortcode('verify', 'shortcode_verify');

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

We took care of that bit already, thank you :)
 
Back
Top