Last active
March 30, 2023 07:18
-
-
Save BFTrick/7040403 to your computer and use it in GitHub Desktop.
Use the gettext WordPress filter to change any translatable string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)