Link to home
Start Free TrialLog in
Avatar of geeta_m9
geeta_m9

asked on

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.
Avatar of eemit
eemit
Flag of Germany image

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

Avatar of geeta_m9
geeta_m9

ASKER

Where is this function located?
In wp-includes/post-template.php
I guess I am not fully comprehending it, but how would it be able to tell which page this content is on?
ASKER CERTIFIED SOLUTION
Avatar of eemit
eemit
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, so do I just need to put the name of the template that is used in the page inside the function?
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

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?
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.
@laughhearty - It's wrong!
@geeta_m9 - you should put the code I posted in each of these 4 Templates.
@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. :)
@laughhearty: Learn first about Custom Page Templates.
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!
My reading comprehension could be a little better too!
;)
Thanks.