How To Password Protect A WordPress Page/Post

How To Password Protect A WordPress Page/Post

WordPress is an open source content management tool. It is written in PHP and MySQL. It is an easy platform to create beautiful sites. WordPress gives another set of powerful privacy feature which lets you create posts for a specific group of people or site editors. In this guide, we will look into different methods on how we can password protect a WordPress page or posts

Prerequisites

Step 1:

Login to your WordPress admin dashboard. Click on Posts and Click on Add New to create a new post.

How To Password Protect A WordPress Page/Post

Step 2:

After writing your new post, you can select your privacy from the Publish menu on the right side of the admin panel.

How To Password Protect A WordPress Page/PostHow To Password Protect A WordPress Page/Post

Click on the Edit button in the Visibility section of the Publish window. On clicking the Edit button, you can select the Password Protected option and set a password to view this post.

Step 3:

After setting the password, when you try to access the post, you will be asked for the password.

How To Password Protect A WordPress Page/Post

The post title will still be visible among other posts or on your site archives. We will learn how to completely hide the post in the next step.

Step 4:

To completely hide the post, we will need to make some change to functions.php file of the active theme. The functions.phpis located inside the theme directory.

/home/sitename/public_html/wp-content/themes/themeName/functions.php

Here, themeName is the name of your theme. It varies according to the theme.

We will add some code to the .php file.

//  Completely Hide protected posts
 
functionexclude_protected($where) {
    global $wpdb;
    return $where .= " AND {$wpdb->posts}.post_password = '' ";
}
 
// display protected posts
functionexclude_protected_action($query) {
    if( !is_single() && !is_page() && !is_admin() ) {
        add_filter( 'posts_where', 'exclude_protected' );
    }
}

Save the changes infunctions.phpfile. This will make your posts completely hidden. You need to share the post url and password if you want someone to view your post.

Conclusion:

In this guide, we learn how to password protect our WordPress posts. We can also password protect the entire WordPress site using third party plugins like SeedProd Coming Soon Pro.

 

Check out the top 3 WordPress hosting services:

A2 Hosting
$2.99 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0

How to change the WordPress Admin login URL

Many wordpress sites are hacked for not being more secured. In this guide we wil
1 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Restrict Bots From Accessing Your WordPress Site

Security has been a major concern these days. This guide will walk though the pr
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Logout All Users In WordPress At Once

What will you do if your WordPress site has been hacked or want all the users to
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Install Google re-CAPTCHA on Your Wordpress Site

In this guide, we will show you how to install re-CAPTCHA on your WordPress site
3 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top