WordPress.org

Ready to get started?Download WordPress

Forums

Password hint for protected page (7 posts)

  1. picarda
    Member
    Posted 3 years ago #

    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');
    ?>

  2. Curtiss Grymala
    Member
    Posted 3 years ago #

    So, what's your question? Are you asking how you should modify the code you provided in order to have it display an error?

  3. picarda
    Member
    Posted 3 years ago #

    Hello Curtiss,
    Yes, like: "you enter the wrong password try again"
    Something simple like this...

    Thanks!

  4. Curtiss Grymala
    Member
    Posted 3 years ago #

    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.

  5. picarda
    Member
    Posted 3 years ago #

    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.

  6. Curtiss Grymala
    Member
    Posted 3 years ago #

    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');
    ?>
  7. picarda
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic