Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use the gettext WordPress filter to change any translatable string.
<?php
/**
* Change text strings
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Sale!' :
$translated_text = __( 'Clearance!', 'woocommerce' );
break;
case 'Add to cart' :
$translated_text = __( 'Add to basket', 'woocommerce' );
break;
case 'Related Products' :
$translated_text = __( 'Check out these related products', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
@wizard247

This comment has been minimized.

Copy link

@wizard247 wizard247 commented May 11, 2017

Thank you! I'm trying to adjust this to be conditional on the product category but not having much luck.

This function works but it applies the change to all categories. I want the Accessories category to have "No." and two other category (ready-to-ship-bows and custom-built-bows) to have "Bow:". Can't seem to make it work and have tried several options found on the net but as I'm not a coder, I'm kinda touching in the dark a little. Not afraid to experiment though!

`function accessories_sku_change ( $translated_text, $text, $domain ) {

if( $domain == 'woocommerce' && ( is_product() && has_term( 'accessories' ) ) )
    return;
    
switch ( $translated_text ) {
    case 'SKU' :
        $translated_text = __( 'No.', 'woocommerce' );
        break;
    case 'SKU:' :
        $translated_text = __( 'No.', 'woocommerce' );
        break;
    }

return $translated_text;    

}

add_filter( 'gettext', 'accessories_sku_change', 30, 3 );`

@makeonlineshop

This comment has been minimized.

Copy link

@makeonlineshop makeonlineshop commented Jan 8, 2019

Thank you !

@hanzlaramey

This comment has been minimized.

Copy link

@hanzlaramey hanzlaramey commented Apr 2, 2019

hello, i got the code for changing the variable product add to cart alert without selecting any variation. i need to make it work for diffrent products can you please help me out in this.

add_filter( 'gettext', 'customizing_variable_product_message', 97, 3 );
function customizing_variable_product_message( $translated_text, $untranslated_text, $domain )
{
if ($untranslated_text == 'Please select some product options before adding this product to your cart.') {
$translated_text = __( 'Here goes your custom text', $domain );
}
return $translated_text;
}

@sohan5005

This comment has been minimized.

Copy link

@sohan5005 sohan5005 commented Apr 21, 2020

Translation functions like _(), n(), esc_html(), x() or any other xx() functions should not be used inside gettext filter. It may end up infinite loop.

@naiaramooy

This comment has been minimized.

Copy link

@naiaramooy naiaramooy commented May 10, 2020

Hello, is it possible to apply this function so that the strings according to the language with WPML? Could you say how to apply it? Thank you.

@glunkov

This comment has been minimized.

Copy link

@glunkov glunkov commented Nov 13, 2020

I'd recommend avoiding this filter, if possible, especially if it's used in several places through code, cause:
IMPORTANT: This filter is always applied even if internationalization is not in effect, and if the text domain has not been loaded. If there are functions hooked to this filter, they will always run. This could lead to a performance problem. (source: https://developer.wordpress.org/reference/hooks/gettext/#more-information )
Depending on situation, sometimes it's better to place modified template file to theme's folder (if plugin allows, like woocommerce), or use a special filter (if plugin allows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment