Feb 022014

How to password protect WordPress pages will show you how to password protect pages by adding a function to a custom template.

Last Updated: March 6, 2019

Latest News: I updated the documentation.

I have tested this code with WordPress 5.0.3 and it works.

This tutorial shows you how to protect pages and redirect the log in user. There are two steps carried out. Step 1, if the log in user is an administrator they will be redirected to the homepage “wp-admin”. Step 2, if it is a non administrator they will be redirect to a page of your choice or the homepage.

This tutorial shows you how to add a function to your theme functions.php file and some code to a custom template you will create. This is one method of protecting your pages without using a plugin. Your website will always perform better and faster with less plugins running. If you don’t like working with code, you can use the password protection feature found in WordPress. This also does a great job in password protecting your pages and posts.

Note: Always work with a child theme. I am using Suffusion Child Theme for this tutorial and also because Suffusion has different types of templates.

Credit is given to:

How To Password Protect WordPress Pages

Step 1 ) Before you begin make sure you have the following settings enabled in WordPress, anyone can register and set it to the lowest role which is Subscriber. See image below.


Step 2 ) Add the following filter code into the functions.php file found in your themes folder. The following filter will direct all non administrators to a password protected page of your choice and the administrator to the admin dashboard “wp-admin”.

Important: I have moved the add filter outside the function

// Redirect admins to the dashboard and other users elsewhere

function my_login_redirect( $redirect_to, $request, $user ) {
// Is there a user?
if ( is_array( $user->roles ) ) {
// Is it an administrator?
if ( in_array( ‘administrator’, $user->roles ) )
return home_url( ‘/wp-admin/’ );
// return home_url();
return get_permalink( 83 );

add_filter( ‘login_redirect’, ‘my_login_redirect’, 10, 3 );

Remember to change the following ID ( 83 ) with the ID of the page you want all your users to be redirected too. If you want them redirected to the home page un-comment return home_url(); by removing the // slashes from the code above.

Note: You can use the code illustrated in the wordpress.org codex link above if you want.

Step 3 ) Now you need to create a custom template and upload the template into our themes folder. Some themes have more than one template. You can choose which ever template you prefer. Copy and paste the content from one template into the new custom template you will create (Remember to give this template a unique name). When you create a new page or if you already have a page you want to protect, simply choose the custom template you just created.

Step 3-a ) Add the following code to the top of the custom template you just created in the previous step. Credit is given to the following website for the following code require-login-for-wordpress-pages.

<?php if(is_user_logged_in()):?>

Step 3-b ) Add the following code at the end of the custom template you created in the previous step.

Note: If you are using a sub domain make sure you point to the correct path or else you see an error message.

<?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>.’);

Step 3-c ) The following image Page Attributes allows you to select a different template for the page if your theme allows it. This is where you will select the custom template you created above. This admin widget is located in the page you are editing. The image is displaying Default Template.

Note: Some themes only have one template.

Step 4 ) The following image shows you the message displayed when someone lands on the protected page. They have two options, click on log in or click on register free here.

login screen password protect page


That is how simple it is to protect a page in your WordPress site.

Note: You might like to read the following documentation Using Password Protection to learn more about password protecting a post. 

If you want more functionality and more control, I recommend the following plugins. Each plugin works differently and has its own functionality and capabilities. Choose the one that best works for you.

Plugins I recommend

I hope you enjoy this tutorial and it helps you. I will be updating this tutorial from time to time to stay up to date.


WordPress Tutorial List:

Manuel Ballesta Ruiz is a web developer, Blogger and WordPress Enthusiast.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>