Solved

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

Posted on 2015-01-28
15
145 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Now that you've installed WordPress 2.9 (http://www.experts-exchange.com/articles/Web_Development/Blogs/WordPress/WordPress-2-9-What-to-Expect-When-Upgrading-to-WordPress-2-9.html?) on your site, you need to install some plugins to get the most out …
In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

816 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

10 Experts available now in Live!

Get 1:1 Help Now