<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RLM &#187; Security</title>
	<atom:link href="http://www.rlmseo.com/blog/category/wordpress/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rlmseo.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 03:37:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Require Login for WordPress Pages</title>
		<link>http://www.rlmseo.com/blog/require-login-for-wordpress-pages/</link>
		<comments>http://www.rlmseo.com/blog/require-login-for-wordpress-pages/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 15:16:40 +0000</pubDate>
		<dc:creator>jcrens8392</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[log in]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[require authentication]]></category>
		<category><![CDATA[require login]]></category>
		<category><![CDATA[wp_die]]></category>

		<guid isPermaLink="false">http://www.rlmseo.com/?p=546</guid>
		<description><![CDATA[I recently worked on a project that required the ability to only allowed logged-in users access to a particular page. Here's how I got it done.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.rlmseo.com/wp-content/uploads/2009/10/wordpress-require-login.jpg" rel="wp-prettyPhoto[549]"><img class="aligncenter size-full wp-image-1260" title="WordPress Require Login" src="http://www.rlmseo.com/wp-content/uploads/2009/10/wordpress-require-login.jpg" alt="WordPress Require Login" width="553" height="174" /></a></p>
<p>By default, anyone can view a WordPress page and there are only a few obvious options to change that in the Write page panel. WordPress does afford you the ability to make pages and posts private or password-protected, but neither of those options accomplished what I needed for this particular project.<br />
<span id="more-549"></span></p>
<h2>Private Pages</h2>
<p>The problem with setting a page&#8217;s visibility to &#8220;Private&#8221; is that it is hidden from everyone except site editors and administrators. Private pages are hidden from all other registered users on your site, including not showing up in a pages list. I wanted my page to be visible in a list of pages (i.e. in the sidebar) to everyone who visited the site as well as in the main navigation. Then, when a user clicks to view the page I needed WordPress to check if that visitor was logged in and, if not, to display a notice that this page requires login. Because of these requirements, the &#8220;Private&#8221; visibility level didn&#8217;t suite my purpose.</p>
<h2>Password Protected Pages</h2>
<p>You can also password protect pages, but I didn&#8217;t want logged-in visitors to have to enter a password to view the page, so this option was out as well.</p>
<p>Unfortunately, there are no other options for protecting specific pages available in WordPress (without the use of a plugin) unless you&#8217;re willing to delve into the code a bit. That said, the task is a simple one and I&#8217;ll explain how I did it in the following tutorial.</p>
<h2>Create a new page template</h2>
<p>First of all, you can view an example of what we&#8217;ll accomplish in this tutorial at the homepage for the <a title="Visit the mortgage calculator plugin homepage" href="http://www.mortgagecalculatorplugin.com" target="_blank">WordPress Mortgage Calculator Plugin</a>. This is a site I helped develop with a friend of mine who is working on an Ajax-enabled mortgage calculator plugin for WordPress. Once at the site, click the &#8220;Download&#8221; menu item to see the effect in action.</p>
<ol>
<li>The first step in requiring users to log in to view a page is to create a new page template by making a copy of your existing page.php template. I named mine &#8220;page_loggedin.php&#8221;.</li>
<li>Once you&#8217;ve done that you need to add a name to the template so that WordPress can recognize it as a new page template. Open the new file and add the following to the top (the template name can be anything you want. This is what you&#8217;ll select within the WordPress admin when creating your new page) :<br />
[php]<?php<br />
/*<br />
Template Name: Logged-In Users Page<br />
*/<br />
?>[/php]</li>
<li>Next you&#8217;ll need to add code to check if the user is logged in and, if not, to display an appropriate message to the user. So add this code just beneath the template name code we added in step 2:[php]<?php if(is_user_logged_in()):?>[/php]</li>
<li>Now go to the very bottom of the file and add the following code. This will call the wp_die() function to halt WordPress execution and display a notice to the visitor that he/she needs to first log in to view this page.</li>
<li>[php]<?php else:<br />
wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.&#8217;);<br />
endif;?>[/php]</li>
<li>Save that new page template and upload it to your server.</li>
<li>That&#8217;s all the code we have to write. Next, create your new page and under &#8220;Page template,&#8221; select the new page template we just created.</li>
<li>Save your new page and that&#8217;s it. That page will now appear in any pages lists just like any other page on your site, but when a user who is not logged in clicks through, he/she will receive a notice similar to this:</li>
</ol>
<div id="attachment_1261" class="wp-caption alignleft" style="width: 563px"><a href="http://www.rlmseo.com/wp-content/uploads/2009/10/login-required-wordpress-message.jpg" rel="wp-prettyPhoto[549]"><img class="size-full wp-image-1261" title="Wordpress message that login is required" src="http://www.rlmseo.com/wp-content/uploads/2009/10/login-required-wordpress-message.jpg" alt="Wordpress message that login is required" width="553" height="98" /></a><p class="wp-caption-text">Example of the message you get when trying to view a protected page</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.rlmseo.com/blog/require-login-for-wordpress-pages/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

