仅当输入的密码不正确时才在受密码保护的页面上添加错误消息

时间:2016-11-29 作者:adrian turculet

我知道这已经在其他几个帖子中讨论过了,但似乎没有一个帖子对这个问题有正确的答案/解决方案。我尝试使用其他帖子中提到的所有建议功能,但似乎都不起作用。当引入错误的密码时,什么都不会发生(只是标准重定向到同一页面),唯一接近答案的是以下代码toscho 在此线程上-Add error message on password protected 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;
}
?>
非常感谢您的时间和投入。

2 个回复
SO网友:Michelle

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;
}
SO网友:Antonio Blanco Oliva

对我来说,此代码有效:

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\' );
这些信息是西班牙语的,但很容易更改。

相关推荐

如何在Functions.php中链接style.css

我是WordPress的新手;我刚开始学习WordPress。我想把风格联系起来。函数中的css。php,但我无法解决这里可能存在的问题。谁能给我指出正确的方向吗?指数php<?php get_header(); ?> <?php if ( have_posts() ) { while ( have_posts() ) { the_post();