You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中为Contact Form 7的$errorMessage添加超链接

解决Contact Form 7错误提示添加超链接不生效的问题

问题背景

我用了一段代码防止Contact Form 7重复提交,想在错误提示里加超链接,但怎么都不生效,当前代码示例如下:

/* Custome Codes */
function is_already_submitted($formName, $fieldName, $fieldValue) {
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $atts = array();
    $atts['show'] = $fieldName;
    
    $atts['filter'] = "$fieldName=$fieldValue";
    $atts['unbuffered'] = 'true';
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
        $found = true;
    }
    return $found;
}
 
/**
 * @param $result WPCF7_Validation
 * @param $tag array
 * @return WPCF7_Validation
 */
function my_validate_email($result, $tag) {
    $formName = 'Register / Sign In'; // Change to name of the form containing this field
    $fieldName = 'Email'; // Change to your form's unique field name
    $errorMessage = 'Email has already been registered, Please <a href="https://example.com">Login</a>'; // Change to your error message
        $name = $tag['name'];
    if ($name == $fieldName) {
        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}
// use the next line if your field is a **required email** field on your form
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);

我想实现的错误提示是:Email has already been registered, Please <a href="https://example.com" rel="nofollow noreferrer"> Login </a>,请问怎么解决这个问题?

解决方法

Contact Form 7默认会对错误提示内容做HTML转义,导致你写的链接变成纯文本显示。要让超链接生效,需要做两个修改:

  • 直接用原始HTML写错误信息:不用转义<、>或引号,直接写完整的<a>标签
  • 添加过滤器关闭HTML转义:用wpcf7_display_message过滤器,允许错误信息中的安全HTML标签

修改后的完整代码

/* Custome Codes */
function is_already_submitted($formName, $fieldName, $fieldValue) {
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $atts = array();
    $atts['show'] = $fieldName;
    
    $atts['filter'] = "$fieldName=$fieldValue";
    $atts['unbuffered'] = 'true';
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
        $found = true;
    }
    return $found;
}
 
/**
 * @param $result WPCF7_Validation
 * @param $tag array
 * @return WPCF7_Validation
 */
function my_validate_email($result, $tag) {
    $formName = 'Register / Sign In'; // 替换为你的表单名称
    $fieldName = 'Email'; // 替换为表单中对应的字段名
    // 直接使用原始HTML编写错误信息
    $errorMessage = 'Email has already been registered, Please <a href="https://example.com" rel="nofollow noreferrer">Login</a>'; 
    $name = $tag['name'];
    if ($name == $fieldName) {
        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}
// 如果字段是必填邮箱字段,使用这个钩子
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);

// 添加过滤器允许错误信息中的安全HTML
function allow_html_in_cf7_error_messages($message) {
    return wp_kses_post($message);
}
add_filter('wpcf7_display_message', 'allow_html_in_cf7_error_messages', 10, 1);

代码说明

  • wp_kses_post函数会保留安全的HTML标签(包括<a>),同时过滤掉恶意代码,既实现了超链接效果,又保证站点安全
  • 不用再手动转义HTML字符,直接写入你需要的链接代码即可

内容的提问来源于stack exchange,提问作者park internet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:50:21