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

如何在WordPress的WP-Members表单添加可翻译自定义HTML文本?

How to Add Translatable Custom Text to WP-Members with Polylang

Hey there! Great question—let’s walk through how to get those two pieces of text set up for translation with Polylang, even if you can’t add custom HTML directly in the WP-Members backend. The short answer: Yes, you absolutely can use Polylang’s string translation feature for this—here’s how to do it step by step:

1. Register Your Custom Strings with Polylang

First, you need to tell Polylang about these two text snippets so they show up in the string translation dashboard. Add this code to your theme’s functions.php file (or a custom plugin, which is safer for theme updates):

// Register translatable strings for WP-Members with Polylang
function register_wpmembers_polylang_strings() {
    // Only run if Polylang is active
    if (function_exists('pll_register_string')) {
        // Register the special offers text
        pll_register_string(
            'wp-members-newsletter-offer', // Unique ID (don’t change this later!)
            'Our customers enjoy receiving special offers...', // Default English text
            'WP-Members', // Group name (helps organize in Polylang)
            true // Allow HTML in the text (set this to true if your text has HTML)
        );

        // Register the privacy note text
        pll_register_string(
            'wp-members-privacy-note',
            'Your personal data will be used...',
            'WP-Members',
            true
        );
    }
}
add_action('init', 'register_wpmembers_polylang_strings');

What this does:

  • The unique IDs let Polylang track each string. Keep these consistent—don’t edit them after setting up translations.
  • The group name (WP-Members) will make it easy to find your strings in Polylang’s dashboard later.
  • Setting the last parameter to true ensures any HTML in your original text is preserved in translations.

2. Translate the Strings in Polylang

Once you’ve added the code:

  • Go to Languages > String Translation in your WordPress backend.
  • Use the filter dropdown to select the WP-Members group—you’ll see your two registered strings.
  • Add the translations for each language you support, just like you would for any other translatable content.

3. Replace the Original Text in WP-Members

Now you need to swap out the hardcoded text in the Newsletter radio button with the translated version. There are two scenarios depending on whether you can edit the field in the WP-Members backend:

Scenario A: You can edit the field in WP-Members backend

If you can access the Newsletter field’s settings and edit its option value:

  • If the field supports PHP (some themes/plugins allow this), use Polylang’s output functions directly:
    • For the offers text: <?php pll_e('wp-members-newsletter-offer'); ?>
    • For the privacy text: <?php pll_e('wp-members-privacy-note'); ?>
  • If PHP isn’t allowed, create a quick shortcode to wrap the translation function:
    Add this to your functions.php:
    // Shortcode to call Polylang translated strings
    function pll_translate_shortcode($atts) {
        $atts = shortcode_atts(array(
            'id' => '',
        ), $atts);
        if (empty($atts['id']) || !function_exists('pll__')) return '';
        return pll__($atts['id']);
    }
    add_shortcode('pll_translate', 'pll_translate_shortcode');
    
    Then in the WP-Members field option value, use:
    • [pll_translate id="wp-members-newsletter-offer"]
    • [pll_translate id="wp-members-privacy-note"]

Scenario B: You can’t edit the field in WP-Members backend

If the Newsletter option text is hardcoded and you can’t modify it via the dashboard, use a filter to replace the text dynamically:
Add this code to your functions.php:

// Replace WP-Members default text with Polylang translations
function replace_wpmembers_text_with_translation($translated_text, $original_text, $domain) {
    // Target only WP-Members text domain
    if ($domain !== 'wp-members') return $translated_text;

    // Replace the offers text
    if ($original_text === 'Our customers enjoy receiving special offers...') {
        return pll__('wp-members-newsletter-offer');
    }

    // Replace the privacy note text
    if ($original_text === 'Your personal data will be used...') {
        return pll__('wp-members-privacy-note');
    }

    return $translated_text;
}
add_filter('gettext', 'replace_wpmembers_text_with_translation', 20, 3);

This will automatically swap the original text with your Polylang-translated version wherever it appears in WP-Members.

Final Notes

  • Always back up your site before editing functions.php or adding custom code.
  • If your text includes HTML, make sure the translations also use valid HTML to avoid layout issues.
  • If you ever need to update the original text, edit the string in the pll_register_string function, then re-sync the translations in Polylang’s string dashboard.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:51:45