• Hello,
    I make a page protected by a password.
    I want to modify the form so, when the user enter the wrong password
    he will get an error message instead of a page refresh….

    Here’s the form:

    <?php
    function fb_the_password_form() {
    global $post;
    $label = ‘pwbox-‘.(empty($post->ID) ? rand() : $post->ID);
    $output = ‘<form action=”‘ . get_option(‘siteurl’) . ‘/wp-pass.php” method=”post”>
    <p>’ . __(“My post is password protected. Please ask me for a password:”) . ‘</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__(“Submit”) . ‘” /></p>
    </form>’;
    return $output;
    }
    add_filter(‘the_password_form’, ‘fb_the_password_form’);
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • So, what’s your question? Are you asking how you should modify the code you provided in order to have it display an error?

    Thread Starter picarda

    (@picarda)

    Hello Curtiss,
    Yes, like: “you enter the wrong password try again”
    Something simple like this…

    Thanks!

    Try something like:

    <?php
    function fb_the_password_form() {
    global $post;
    $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
    $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">';
    if( isset( $_REQUEST['post_password'] ) ) {
        $output .= '<p>' . __("The password you entered was incorrect. Please try again:") . '</p>';
    } else {
        $output .= '<p>' . __("My post is password protected. Please ask me for a password:") . '</p>';
    }
    $output .= '<p><label for="' . $label . '">' . __("Password") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
    </form>';
    return $output;
    }
    add_filter('the_password_form', 'fb_the_password_form');
    ?>

    That code should check to see if the form was already submitted (by checking to see if the value post_password form field was sent already) and output different intro text.

    Thread Starter picarda

    (@picarda)

    Thanks Curtis,
    But for a reason I don’t know it doesn’t display the first output message.
    Do we have a problem with the wp-pass.php you think ?

    Thanks again.

    I forgot that WordPress uses an intermediate page in between submitting the form and returning to the protected post/page.

    I think you’ll need to use the referer instead of trying to check the submitted information.

    You might try something like:

    <?php
    function fb_the_password_form() {
    global $post;
    $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
    $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">';
    $prepg = wp_get_referer();
    if( false === $prepg || !stristr( $prepg, 'wp-pass.php' ) ) {
        $output .= '<p>' . __("My post is password protected. Please ask me for a password:") . '</p>';
    } else {
        $output .= '<p>' . __("The password you entered was incorrect. Please try again:") . '</p>';
    }
    $output .= '<p><label for="' . $label . '">' . __("Password") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
    </form>';
    return $output;
    }
    add_filter('the_password_form', 'fb_the_password_form');
    ?>

    Thread Starter picarda

    (@picarda)

    Hello Curtis,
    No such a luck, doesn’t work….
    I don’t even get it why wordpress make this complicated…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Password hint for protected page’ is closed to new replies.