Paulund
2014-03-25 #wordpress

Hide Content on Password Protected Pages

On private websites it can be handy to have password protected pages to stop everyone getting at some pieces of content. In WordPress creating a password protected page is very easy, there is a visibility setting on the publish meta box, when you click edit on here you are presented with 3 options, public, private and password protected. If you choose password protected you simply need to enter a password in the box that appears and this page will now be protected. The next time you visit this page you will be presented with a password form, where the only way you can see this content is if you enter the correct password.

When you want to protect content on the page this is a great feature of WordPress but there is a problem with this functionality, the password protected form will only appear in the content area of your page. Therefore on password protected pages people will still be able to see the title of the post, any custom fields in the template and sidebar widgets. If you have created a protected page then you may also want to hide the sidebar content or custom fields.

To hide other content on template then you need to add some code around the content you want to hide. WordPress has a function called post_password_required(), if the password is not needed or it has already been entered correctly the function will return false, if the password is needed it will return true. We will use this function to hide content on password protected pages, below is an example of how you will hide the sidebar on password protected pages. Put the post_password_required() in a if statement and only show the content if this function returns false.


<?php
if(!post_password_required())
{
    get_sidebar();
}
?>