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

如何通过WordPress的functions.php修改插件<label>元素文本?

WordPress插件特定标签文本替换方案

方法1:利用gettext翻译钩子(优先尝试)

如果插件的“Length (cm)”是通过WordPress标准翻译函数(如__()、_e())输出的,直接用gettext钩子拦截替换:

add_filter( 'gettext', 'replace_calculator_label_text', 20, 3 );
function replace_calculator_label_text( $translated_text, $text, $domain ) {
    // 替换目标字符串,需替换为插件实际文本域(可通过Loco Translate查看插件的文本域)
    if ( $text === 'Length (cm)' && $domain === 'your-plugin-text-domain' ) {
        $translated_text = 'Visina zida';
    }
    return $translated_text;
}

注:把your-plugin-text-domain替换成产品测量计算器插件的真实文本域,可在插件目录的.pot文件或主插件文件的load_plugin_textdomain函数参数中找到。

方法2:输出缓冲全局替换(通用兜底方案)

若方法1无效,说明字符串未走翻译函数,用输出缓冲捕获页面内容后精准替换标签文本:

add_action( 'wp_footer', 'start_calculator_label_buffer' );
add_action( 'wp_footer', 'end_calculator_label_buffer', 999 );

function start_calculator_label_buffer() {
    ob_start( 'replace_calculator_label_content' );
}

function replace_calculator_label_content( $buffer ) {
    // 正则匹配包含目标文本的<label>标签,避免误替换其他内容
    $buffer = preg_replace( '/<label[^>]*>Length \(cm\)<\/label>/i', '<label$1>Visina zida</label>', $buffer );
    return $buffer;
}

function end_calculator_label_buffer() {
    ob_end_flush();
}

注:如果

注意事项

  • 将代码添加到当前主题的functions.php文件,或使用代码片段插件(如Code Snippets)添加,避免主题更新丢失代码。
  • 修改后清空浏览器缓存和WordPress缓存,确保生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 21:28:15