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

Unlimited category passwords requires our PPWP Pro and the custom codes below.

Add this code snippet to your (child) theme’s functions.php file:

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.

Logic & Limitations

  • At the moment, Category Protection is only applicable to posts, not custom post types.
  • For users who have already used and updated our PPWP Lite from version 1.4.1 and before, you will need to enable “Use Custom Form Action” settings option under “Misc” tab for this feature to work properly.

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 August 23, 2021