Password Protect WordPress Categories

Instead of creating an individual password for each post, you can protect all posts under one or multiple categories at once. Once users unlock a post successfully, they will be able to access the rest of the content automatically.

Requirement:

How to password protect post categories

Once activating our PPWP plugin, navigate to Categories page from your admin dashboard.

  1. Enable “Password Protect Categories” option
  2. Select the categories you want to protect
  3. Set a password to unlock all these categories
  4. Save changes

Once protected, all content under these categories, including the new ones, will be hidden with a password form.

How to set unlimited category passwords

To set unlimited passwords for your protected categories, simply add the following code snippet to your (child) theme’s functions.php file or plugins like Code Snippets.

Please note that the function is available in our PPWP Pro version.

function ppw_cs_get_validated_passwords() {

  return [ 'password1', 'password2']; // Add your category passwords here

}


add_filter(

  'ppwp_category_is_valid_password',

  function ( $validated, $args ) {

     if ( $validated ) {
        return $validated;
     }

     $password = $args['password'];

     $validated_passwords = ppw_cs_get_validated_passwords();

     if ( ! in_array( $password, $validated_passwords, true ) ) {
        return false;
     }

     PPW_Category_Service::get_instance()->set_password_to_cookie( $password, PPW_Category_Service::COOKIE_NAME . 'hook', 1 );
     PPW_Category_Service::get_instance()->set_password_to_cookie( $password, PPW_Constants::WP_POST_PASS );

     return true;

  }, 10, 2 );


add_filter(

  'ppw_category_is_valid_cookie',

  function ( $validated ) {

     $cookie_key = PPW_Category_Service::COOKIE_NAME . 'hook' . COOKIEHASH;

     if ( ! isset( $_COOKIE[ $cookie_key ] ) ) {

        return $validated;

     }


     $cookie_value = $_COOKIE[ $cookie_key ];

     $cookie_value = explode( '|', $cookie_value );

     if ( count( $cookie_value ) < 2 ) {

        return $validated;

     }

     $password_hashed = $cookie_value[1];

     $validated_passwords = ppw_cs_get_validated_passwords();

     foreach ( $validated_passwords as $key => $validated_password ) {

        if ( ppw_free_check_password( $validated_password, $password_hashed ) ) {
           return true;
        }
     }

     return $validated;
  }

);

To protect multiple categories separately, please use the category protection feature provided by our Access Levels extension instead.

How to protect whole category pages

Since Password Protect WordPress Pro version 1.3.8, you can protect whole category pages.

To do that, simply add the following custom code to your (child) theme’s functions.php file or Code Snippet plugin.

add_filter( 'ppwp_enable_category_page_protection', '__return_true' );

Logic & Limitations

  • At the moment, Category Protection is only applicable to posts, not custom post types.
  • You need to refresh the Categories page for new categories to be shown in “Select your private categories” field.

Customize password form

You can customize password form via WordPress Customizer as well as choose to show post excerpts while protecting the content.

Lasted updated on March 24, 2022