admin管理员组

文章数量:1133941

I know this has been discussed in a couple of other posts but none of them seem to have the correct answer/solution to the question. I tried using all the suggested functions mentioned in the other posts but none seem to work. When a wrong password is introduced nothing happens(just the standard redirect to the same page) The only one getting close to the answer is the following code provided by toscho on this thread - Add error message on password protected page but unfortunately the error message shows regardless if the password was not introduced yet or as soon as you land on the page:

<?php
add_filter( 'the_password_form', 'wpse_71284_custom_post_password_msg' );
function wpse_71284_custom_post_password_msg( $form )
{
// No cookie, the user has not sent anything until now.
if ( ! isset ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) )
    return $form;

// Translate and escape.
$msg = esc_html__( 'Sorry, your password is wrong.', 'your_text_domain' );

// We have a cookie, but it doesn’t match the password.
$msg = "<p class='custom-password-message'>$msg</p>";

return $msg . $form;
}
?>

Your time and input is much appreciated.

I know this has been discussed in a couple of other posts but none of them seem to have the correct answer/solution to the question. I tried using all the suggested functions mentioned in the other posts but none seem to work. When a wrong password is introduced nothing happens(just the standard redirect to the same page) The only one getting close to the answer is the following code provided by toscho on this thread - Add error message on password protected page but unfortunately the error message shows regardless if the password was not introduced yet or as soon as you land on the page:

<?php
add_filter( 'the_password_form', 'wpse_71284_custom_post_password_msg' );
function wpse_71284_custom_post_password_msg( $form )
{
// No cookie, the user has not sent anything until now.
if ( ! isset ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) )
    return $form;

// Translate and escape.
$msg = esc_html__( 'Sorry, your password is wrong.', 'your_text_domain' );

// We have a cookie, but it doesn’t match the password.
$msg = "<p class='custom-password-message'>$msg</p>";

return $msg . $form;
}
?>

Your time and input is much appreciated.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Nov 29, 2016 at 16:23 adrian turculetadrian turculet 113 bronze badges 2
  • Did you see the comment on Toscho's answer from that post? It might explain what you're seeing & provide a solution. – Michelle Commented Nov 29, 2016 at 21:08
  • Hi @Michelle , the comment was saying they used (wp_get_referer() == get_permalink()) to avoid the error message being displayed all the time but I am not sure where to introduce that code. Would you be able to advise? – adrian turculet Commented Nov 30, 2016 at 13:54
Add a comment  | 

2 Answers 2

Reset to default 1

Try this:

add_filter( 'the_password_form', 'wpse_71284_custom_post_password_msg' );

/**
 * Add a message to the password form.
 *
 * @wp-hook the_password_form
 * @param   string $form
 * @return  string
 */
function wpse_71284_custom_post_password_msg( $form )
{
    // No cookie, the user has not sent anything until now.
    if ( ! isset ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) )
        return $form;

    // No cookie, person just submitted the form on this page and got the password wrong
    if (wp_get_referer() == get_permalink()) {
      // Translate and escape.
      $msg = esc_html__( 'Sorry, your password is wrong.', 'your_text_domain' );

      // We have a cookie, but it doesn’t match the password.
      $msg = "<p class='custom-password-message'>$msg</p>";
    } else {
      $msg = "";
    }
    return $msg . $form;
}

for me, this code works:

function my_password_form() {
     global $post;

     $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );

     $passwordProtectedPageURL = get_the_permalink();
     $wrongPassword = ' ';

     if( ( sanitize_text_field( $_SERVER["HTTP_REFERER"] ) === $passwordProtectedPageURL ) &&  isset ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] )){
          $wrongPassword = '<span style="color:#ed1b24;font-weight:bold;">Contraseña incorrecta, por favor inténtalo de nuevo.</span>';
          setcookie ('wp-postpass_' . COOKIEHASH, null, -1);

     }


     $form = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post" class="post-password-form">
' . "<p>Este contenido está protegido por contraseña. Para verlo, por favor, introduce tu contraseña a continuación:</p>" . '
<p><label for="' . $label . '">' . __( "Password:" ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /></label>
<input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" /></p>
</form><p>' . $wrongPassword . '</p>';

     return $form;
}
add_filter( 'the_password_form', 'my_password_form' );

The messages are in spanish, but they are easy to change.

本文标签: functionsAdd error message on password protected page ONLY when password introduced was incorrect