Solved

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

Posted on 2015-01-28
15
147 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
text on slider doesn't resize nicely 5 72
WordPress Themes 10 45
Disabling the Main Menu for a Certain Page 2 40
Wordpress Query 5 24
Who says nothing in life is free? WordPress.com is a freebie. WordPress.org's downloadable publishing platform is free. Heck, even WordPressMU is free. WordPress is an open source project, which means it can be used on any personal or commerc…
How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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.

791 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