1

I have a password protected page (Page X), but I need the password form that grants access to Page X to live on the home page.

With the password form on the home page, I want the user to be able to enter the correct password directly. If correct, I want the user to be redirected to the password protected Page X.

Is this possible to implement? Normally, once a user presses on a link that is password protected, they are redirected to the password form that is its own separate page. But I want to cut out this intermediary password form page, and place the form directly on the home page. If you have any tips, please let me know.

I tried using the get_the_password_form function below and passing in the page/post ID. It does return the proper HTML code for a form, and a form is displayed. However, it does not function correctly: does nothing despite entering in the correct password. The function below is from wp-includes folder.

function get_the_password_form( $post = 0 ) {
    $post = get_post( $post );
    $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
    $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
    <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
    <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
    ';

    /**
     * Filters the HTML output for the protected post password form.
     *
     * If modifying the password field, please note that the core database schema
     * limits the password field to 20 characters regardless of the value of the
     * size attribute in the form input.
     *
     * @since 2.7.0
     *
     * @param string $output The password form HTML output.
     */
    return apply_filters( 'the_password_form', $output );
}

1 Answer 1

2

The problem is that WP will redirect to the referrer when the authentication succeeds.

Fortunately, you can also supply _wp_http_referer and it will be preferred over the HTTP Referrer, so we need to add a hidden field to the form and fill it with the permalink of the post in question:

echo str_replace('</form>', '<input type="hidden" name="_wp_http_referer" value="' . esc_attr(get_permalink(123)) . '" /></form>', get_the_password_form(123));

with 123 being the post ID.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.