Skip to content
UFO Performance Marketing

Where intelligence is put to the test.

HomeServicesOur WorkBlogAboutContact Us
MenuClose

Explore UFO

HomeServicesOur WorkBlogAboutContact Us

Where intelligence is put to the test.

WordPress · Oct 16, 2014

Require Login for WordPress Pages

Require Login for WordPress Pages
JC
John Crenshaw
Oct 16, 2014

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.

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.

Why Not Private Pages?

The problem with setting a page's visibility to "Private" 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 "Private" visibility level didn't suite my purpose.

Why Not Password Protected Pages?

You can also password protect pages, but I didn't want logged-in visitors to have to enter a password to view the page, so this option was out as well.

Unfortunately, there are no other options for protecting specific pages available in WordPress (without the use of a plugin) unless you're willing to delve into the code a bit. That said, the task is a simple one and I'll explain how I did it in the following tutorial.

Create a new page template

  1. 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 "page_loggedin.php".
  2. Once you'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'll select within the WordPress admin when creating your new page) :
1
2
3
4
5
<?php
/*
Template Name: Logged-In Users Page
*/
?>
  1. Next you'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
1
<!--?php if(is_user_logged_in()):?-->

‍

  1. 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.
1
2
3
<?php else:
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>.');
endif; ?>
  1. Save that new page template and upload it to your server.
  2. That's all the code we have to write. Next, create your new page and under "Page template," select the new page template we just created.
  3. Save your new page and that'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:
Wordpress message that login is required
Example of the message you get when trying to view a protected page
WordPress

Get the latest from UFO

Keep reading

More from the blog

All Posts →
Using the "More" Tag on WordPress Pages
WordPress

Using the "More" Tag on WordPress Pages

How to use the "more" tag in the blog section to accomplish the standard blog setup with the beginning of a post on the homepage and the rest on the single post page, with a "Read more" link allowing the reader to click through to the latter.

John Crenshaw · Oct 16, 2014
Using WordPress Custom Fields
WordPress

Using WordPress Custom Fields

Using WordPress as a CMS often for client sites I use custom fields perhaps quite a bit more than the regular WordPress user.

John Crenshaw · Oct 16, 2014
Auto Create Navigation Tabs for New Pages
WordPress

Auto Create Navigation Tabs for New Pages

In this article I explain how you can setup your blog to automatically create main navigation links/tabs when new pages are published by using custom fields to mark those pages you want to appear in the navigation menu.

John Crenshaw · Oct 16, 2014

Want results like these?

Let's talk about where you are, where you want to go, and the smartest way to get there.

Contact Us
UFO

Where intelligence is put to the test.

  • Home
  • Services
  • Our Work
  • Cool People Saying Smart Things
  • About
  • Partners
  • Testimonials
  • Blog
  • Marketing Library
  • Agency Guides
  • Careers
  • Contact Us

Offices

  • Cincinnati, OH
  • Columbus, OH
  • Portland, OR
  • (513) 549-7355
Contact Us
Google 5.0 five star rating Google Ads Premier Partner 2026
© Copyright 2026 UFO Marketing. All rights reserved. Privacy Policy | Terms of Use