How do I protect the PHP content on a WordPress page that is password protected?

I would like to password protect the content on a WordPress page. However, when I do so, only the content that was entered into the WordPress text editor is hidden. If I use a PHP template on the page, the content from the template still shows.  How can I ensure that this content also remains hidden?

Thank you.
geeta_m9Asked:
Who is Participating?
 
eemitCommented:
In Page Edit Screen in Page Attributes/Template you can see which Page Template is used. Then change your PHP Page Template as I have posted above.
If you  need more help please visit my profile page and click the “Hire Me” button.
0
 
eemitCommented:
Wrap your page template code in this conditional statement (except your code that displays page content added in WP Dashboard).
Note: post_password_required() WordPress function checks both whether your post requires a password and whether the correct password has been provided:
if ( ! post_password_required() ) {
	// Here your Template code
}

Open in new window

0
 
geeta_m9Author Commented:
Where is this function located?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
eemitCommented:
In wp-includes/post-template.php
0
 
geeta_m9Author Commented:
I guess I am not fully comprehending it, but how would it be able to tell which page this content is on?
0
 
geeta_m9Author Commented:
Ok, so do I just need to put the name of the template that is used in the page inside the function?
0
 
eemitCommented:
geeta_m9,
Please never change any WordPress Core files.
You should put this in your PHP Page Template:
if ( ! post_password_required() ) {
	// Here your PHP code that displays the content from the page template
	// which still shows, to ensure that this content also remains hidden
}

Open in new window

0
 
geeta_m9Author Commented:
Ok, assuming this will work for one template. But I have  four different templates on four pages each with its own code that I also wish to keep private. How would I put them all into this function?
0
 
Alicia St RoseOwner & Principle Developer/DesignerCommented:
All Pages will use the page.php template. If you don't have page.php, you can include the code in index.php.

If this is confusing, perhaps a peek at the WordPress template hierarchy may help.
0
 
eemitCommented:
@laughhearty - It's wrong!
@geeta_m9 - you should put the code I posted in each of these 4 Templates.
0
 
Alicia St RoseOwner & Principle Developer/DesignerCommented:
@eemit @geeta_m9: I'm heading to the sidelines to see how this plays out. Especially, the 4 templates. Not sure what that's referencing. Maybe I'm going to learn somthing. :)
0
 
eemitCommented:
@laughhearty: Learn first about Custom Page Templates.
0
 
Alicia St RoseOwner & Principle Developer/DesignerCommented:
Actually, I'm a Custom themer so I know them intimately and use them all the time in my themes.

Now that it is clear to me what "PHP template" was referring to, then you are correct. You'd definitely put that code into each Custom Page Template.

Whew, thought I'd missed something!
0
 
Alicia St RoseOwner & Principle Developer/DesignerCommented:
My reading comprehension could be a little better too!
;)
0
 
geeta_m9Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.