Password Protect content() on homepage

admin2025-06-06  2

I need to password protect several pages and various areas of my templates if the page had a password enabled. This I can do on all subpages but on the homepage it's just not working.

I do not want to whole site password protected, which many plugines seem to do - the built-in WP password protection is fine for this purpose apart from the homepage issue. When the visitor lands on the site, they can see elements of the page but some content is hidden. The content needs to be unlocked by just a password and not a registered WP account username/password.

I assume the page used as home is treated differently than "normal" pages.

The only thing I can think of is to instantly redirect the homepage to a "normal" pge that looks identical - then the password protection function would work?

Google just offers either protecting the whole site (ie all you see in a login page) or protecting a normal page or using registered accounts to unlock content.

I need to password protect several pages and various areas of my templates if the page had a password enabled. This I can do on all subpages but on the homepage it's just not working.

I do not want to whole site password protected, which many plugines seem to do - the built-in WP password protection is fine for this purpose apart from the homepage issue. When the visitor lands on the site, they can see elements of the page but some content is hidden. The content needs to be unlocked by just a password and not a registered WP account username/password.

I assume the page used as home is treated differently than "normal" pages.

The only thing I can think of is to instantly redirect the homepage to a "normal" pge that looks identical - then the password protection function would work?

Google just offers either protecting the whole site (ie all you see in a login page) or protecting a normal page or using registered accounts to unlock content.

Share Improve this question asked Nov 21, 2018 at 13:14 CuCoCuCo 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can make the homepage password protected by creating a page and selecting it in the admin Settings > Reading > Homepage Displays. Then on the page settings make it password protected.

Then create a template called front-page.php and use conditionals to show/hide the content.

For Example...

if ( ! post_password_required( $post ) ) {
    // Password Protected Content
}else{
    // Password Form
    echo get_the_password_form();
}

More info on password protected pages.

Note: If you have already entered your password, you can delete your cookies and refresh the page to see the password form again.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749156301a316843.html

最新回复(0)