【问题标题】:Where is the "Invalid username" WooCommerce error message coming from?“无效的用户名”WooCommerce 错误消息来自哪里?
【发布时间】:2018-11-02 09:38:19
【问题描述】:

我需要修改 WooCommerce 错误消息的文本(出于安全原因),但似乎无法找到该特定消息的实际来源。

我在example.com/shop/my-account/ 页面上(也可能是example.com/my-account/)并故意输入错误的用户名和密码进行测试。

相关的 HTML 部分看起来完全像这样:

<ul class="woocommerce-error" role="alert">
    <li>
        <strong>ERROR</strong>
        : Invalid username.
        <a href="http://example.com/shop/my-account/lost-password/">Lost your password?</a>
    </li>
</ul>

我需要将消息的Invalid username 部分更改为其他内容。

所以,我搜索了那个确切的字符串,但只能找到 Invalid username or email. 的实例这显然与我要修改的错误消息不匹配

然后我检查了整个 HTML 部分是在哪里生成的,发现它是在 plugins\woocommerce\templates\notices\error.php 中生成的。

以下是相关部分:

<ul class="woocommerce-error" role="alert">
    <?php foreach ( $messages as $message ) : ?>
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php endforeach; ?>
</ul>

因此,该错误消息来自 $messages 数组。

但是我在哪里可以找到有问题的实际消息?

(这样我可以修改它)

为什么找不到完全匹配的 (Invalid username)?

当然,我尝试搜索$messages,但找不到任何相关内容。那么,那个特定的错误消息到底是从哪里来的呢?即使在整个 wp-content 文件夹中搜索也不会找到任何地方。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我相信,有问题的实际消息来自 WordPress 本身,而不是 WooCommerce。具体尝试查看 wp-includes/user.php。如果您想更改消息,我强烈建议您使用挂钩来这样做,以防止在 WP 更新时丢失您的修改。

    【讨论】:

    • 是的,你完全正确!在第 147 行的 wp-includes/user.php 中是:&lt;strong&gt;ERROR&lt;/strong&gt;: Invalid username. 有趣的是,在 wp-includes/pluggable.php 中还有这个错误消息:&lt;strong&gt;ERROR&lt;/strong&gt;: Invalid username, email address or incorrect password.,这正是我想要避免给潜在黑客提供有价值信息的错误消息。是的,我知道,更改核心文件会在下次更新时恢复所有内容。
    【解决方案2】:

    我知道这篇文章很旧,但如果它可以帮助某人,我认为您需要修改的代码位于文件“woocommerce/includes/class-wc”中的函数“process_login()”中Woocommerce 插件的-form-handler.php":

    if ( is_wp_error( $user ) ) {
        $message = $user->get_error_message();
        $message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', $message );
        throw new Exception( $message );
    } else {
    

    您可以在抛出异常之前更改消息。(顺便说一下,我不明白 str_replace 的附加值,因为它什么都不做)。

    欢迎评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2019-07-14
      • 1970-01-01
      • 2013-10-06
      • 2011-02-18
      相关资源
      最近更新 更多