Skip to main content

Add Password Protection to Any WordPress Page

Mon, Jun 6th, 2022

Want to create a membership site or just protect some content? Learn how to password protect any page on your WordPress site with this simple code.

WordPress is a great content management system because it is so versatile. You can use it for a simple blog or a complex website with many different pages and features. One of the great things about WordPress is that you can easily protect any page on your site with a password. This can be useful if you want to limit access to certain pages to specific users. For example, you might want to password protect a page that contains sensitive information. To do this, simply add the following code to your functions.php file.

This will add password protection to any page on your WordPress site and will redirect users to log in to get access. This is a great way to keep your site secure and ensure that only authorized users can access certain pages.

add_action( 'template_redirect', function() {
  if ( is_user_logged_in() || ! is_page() ) return;
  $restricted = array( PAGE ID ); // all your restricted pages
  if ( in_array( get_queried_object_id(), $restricted ) ) {
    wp_redirect( site_url( 'LOGIN URL' ) );