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

WPCS兼容验证:含gettext占位符的函数触发PHPCS报错求助

Fixing "Strings should have translatable content" PHPCS Error for Gettext Placeholders in WPCS

Hey there! Let's tackle this WPCS compatibility issue you're facing. That "Strings should have translatable content" error usually pops up when PHPCS doesn't recognize that your string with placeholders is properly set up for translation in WordPress. Here are the most common fixes and things to check:

Common Causes & Solutions

1. You're not wrapping the string in WordPress' gettext functions

WordPress requires using specific translation functions (like __(), _e(), _x()) to mark strings as translatable. If you have a string with placeholders but it's not inside one of these functions, PHPCS will flag it.

Wrong:

$message = "Hello %s, welcome back!";

Right:

$message = __("Hello %s, welcome back!", "your-plugin-text-domain");

2. Missing or incorrect text domain

Every translation function needs a second parameter: your plugin's unique text domain. Omitting this tells WPCS the string isn't properly registered for translation.

Wrong:

$message = __("Hello %s, welcome back!"); // No text domain

Right:

$message = __("Hello %s, welcome back!", "my-awesome-plugin");

Make sure your text domain matches the one you've declared in your plugin header and loaded with load_plugin_textdomain().

3. Using non-standard placeholders

WPCS expects standard sprintf-style placeholders (%s, %d, %f) instead of custom syntax like {name} or {{user}}. Stick to the standard format so PHPCS recognizes the string as translatable.

Wrong:

$message = __("Hello {username}, welcome back!", "my-awesome-plugin");

Right:

$message = sprintf(
    __("Hello %s, welcome back!", "my-awesome-plugin"),
    esc_html($username)
);

4. String concatenation instead of sprintf

If you're building the string by concatenating static text and variables, PHPCS can't recognize the full string as translatable. Always use sprintf() with a wrapped translatable base string.

Wrong:

$message = "Hello " . $username . ", welcome back!";
// Or even this, which still concatenates
$message = __("Hello ") . $username . __(", welcome back!", "my-awesome-plugin");

Right:

$message = sprintf(
    __("Hello %s, welcome back!", "my-awesome-plugin"),
    esc_html($username)
);

Additional Checks

  • Ensure you've loaded your plugin's text domain correctly in your main plugin file:
    function my_plugin_load_textdomain() {
        load_plugin_textdomain(
            'my-awesome-plugin',
            false,
            dirname(plugin_basename(__FILE__)) . '/languages/'
        );
    }
    add_action('plugins_loaded', 'my_plugin_load_textdomain');
    
  • Double-check that your WPCS ruleset is up to date—outdated rules might have false positives.
  • If the string is inside a class or function, confirm the text domain is accessible and correctly referenced.

If you still run into issues, share a snippet of the problematic code and I can help narrow it down further!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:02:17