How to Create a Central Password Login Page for Protected WordPress Content

To grant access to protected content, you need to provide your users with a password and the page URL. But what if you have a lot of password protected content and have to send individual content to each user?

Password Suite extension comes with a simple shortcode allowing you to create a central password form. Instead of handling multiple protected content URLs, you just need to send your users a single link. And they will be redirected to the pre-defined content based on the entered password after login.

Requirements:

How to create a central login form

You can add the central password form anywhere in your site using the shortcode:

[ppwp_central post_ids="x, x, x"]

Replace x by the post id of the content where your users will be redirected after entering the corresponding password.

Find the id of a post by simply hovering over its title. A link containing the post id will be shown at the bottom of the screen. In the example below, you can see the post ID is 1.

Let’s say you want to redirect users to “Hello world!” page. Your shortcode should be something like [ppwp_central post_ids= "1"].

You can also style this password form, such as add headline, edit the description, change color with WordPress Customizer.

How PPWP central login form works

Let’s say there are 2 password protected posts: A and B.

  • Post A has post_id = 1 and is protected with password 111
  • Post B has post_id = 2 and is protected with password 222

We create a central login page using shortcode [ppwp_central post_ids= "1, 2"]

  • When users input password 111 into password form, they will be redirected to post A. On the other hand, inputting password 222 to let them see post B.
  • In case post A and post B use the same password, e.g. 000. Entering this password will direct users to the first matching page that plugin finds. So you’re recommended to use different passwords for each protected content.
  • Once users have entered a password to access protected content, our plugin will store a cookie containing the password into their browser. So they can re-visit the content multiple times without proving a password until the cookie expires. Please note that cookies won’t be updated if users use another password to access protected content.

Limitation: The central login page might not work properly when Unlock Protected Content without Page Refresh option is enabled.

Redirect users to central login form

Instead of showing the password form, you can redirect users to the central login form if they access password protected content directly.

To do so, add the following code snippet into your (child) theme’s functions.php file.

For all password protected content

add_action(
    'template_redirect',
    function () {
        $post_id = get_the_ID();
        if (!post_password_required($post_id)) {
            return;
        }
        wp_redirect('your-central-login-page-url');
        exit;
    },
    100
);

For certain content only

/* Redirect users to your-central-login-page-url if they access the content with post id 12 or 13 directly */
add_action(
    'template_redirect',
    function () {
        $post_id = get_the_ID();
        $post_ids = [12, 13];
        if (!in_array($post_id, $post_ids)) {
            return;
        }
        if (!post_password_required($post_id)) {
            return;
        }
        wp_redirect('your-central-login-page-url');
        exit;
    },
    100
);
Lasted updated on February 24, 2021