如何通过Polylang动态修改WooCommerce的「前往结算」按钮文本?
Hey there! Let me walk you through a better approach for translating your WooCommerce checkout button text using Polylang—your current method works, but it can be more maintainable and aligned with how Polylang is designed to handle translations.
Issues with Your Current Code
- Hardcoded translations: Every time you add a new language or need to tweak the text, you have to edit the code snippet, which isn't ideal for long-term maintenance.
- Locale dependency: Using
get_locale()relies on WordPress's locale settings, but Polylang often uses shorter language slugs (likefrinstead offr_FR) as its primary language identifiers, which could lead to mismatches if your Polylang setup uses different locale codes.
Solution 1: Use Polylang's Native Language Detection
Polylang provides the pll_current_language() function to reliably get the active language slug (e.g., fr, en, zh). Here's how to adjust your code:
add_filter('woocommerce_button_proceed_to_checkout', 'replace_checkout_text_with_polylang'); function replace_checkout_text_with_polylang() { $current_lang = pll_current_language(); switch($current_lang) { case 'fr': return 'Ajoutez au devis'; case 'en': return 'Add to quotes'; case 'zh': return '索取报价'; default: // Fall back to WooCommerce's default checkout text if no match return __('Proceed to checkout', 'woocommerce'); } }
This is a direct improvement over your original code because it uses Polylang's own language detection, avoiding any locale mismatches.
Solution 2: Use Polylang's String Translation (Best Practice)
For a more scalable approach, register the checkout button text as a translatable string in Polylang. This lets you manage translations directly from the WordPress admin without touching code:
Step 1: Register the string with Polylang
add_action('init', 'register_checkout_button_translatable_string'); function register_checkout_button_translatable_string() { // Register the default WooCommerce checkout text as a translatable string pll_register_string( 'woocommerce_proceed_to_checkout', // Unique string identifier __('Proceed to checkout', 'woocommerce'), // Original text 'WooCommerce' // Group name for organization in Polylang ); }Step 2: Filter the button text to use Polylang's translation
add_filter('woocommerce_button_proceed_to_checkout', 'translate_checkout_button_via_polylang'); function translate_checkout_button_via_polylang($original_text) { // Return the translated string from Polylang return pll__('woocommerce_proceed_to_checkout'); }
Once you add these snippets, go to Polylang → String Translations in your WordPress admin. You'll find the "woocommerce_proceed_to_checkout" entry under the "WooCommerce" group—here you can set the translation for each language you've configured in Polylang. This method is far more maintainable, especially if you plan to add more languages later.
Quick Notes
- Make sure Polylang is fully activated before using these functions (they're only available when Polylang is loaded).
- If you're using the Snippets plugin, just create new snippets for each code block above and activate them.
内容的提问来源于stack exchange,提问作者Elbarbu

