Solved

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

Posted on 2015-01-28
15
140 Views
Last Modified: 2015-02-06
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.
0
Comment
Question by:geeta_m9
  • 6
  • 5
  • 4
15 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 40576186
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
 

Author Comment

by:geeta_m9
ID: 40576189
Where is this function located?
0
 
LVL 15

Expert Comment

by:eemit
ID: 40576219
In wp-includes/post-template.php
0
 

Author Comment

by:geeta_m9
ID: 40576236
I guess I am not fully comprehending it, but how would it be able to tell which page this content is on?
0
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 40576270
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
 

Author Comment

by:geeta_m9
ID: 40576294
Ok, so do I just need to put the name of the template that is used in the page inside the function?
0
 
LVL 15

Expert Comment

by:eemit
ID: 40576341
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:geeta_m9
ID: 40576356
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
 
LVL 7

Expert Comment

by:Alicia St Rose
ID: 40577485
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
 
LVL 15

Expert Comment

by:eemit
ID: 40577573
@laughhearty - It's wrong!
@geeta_m9 - you should put the code I posted in each of these 4 Templates.
0
 
LVL 7

Expert Comment

by:Alicia St Rose
ID: 40578416
@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
 
LVL 15

Expert Comment

by:eemit
ID: 40578581
@laughhearty: Learn first about Custom Page Templates.
0
 
LVL 7

Expert Comment

by:Alicia St Rose
ID: 40578792
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
 
LVL 7

Expert Comment

by:Alicia St Rose
ID: 40578837
My reading comprehension could be a little better too!
;)
0
 

Author Closing Comment

by:geeta_m9
ID: 40594058
Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now