Solved

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

Posted on 2015-01-28
15
148 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
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 make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…

733 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