Skip to content

Instantly share code, notes, and snippets.

@vishalkakadiya
Created March 18, 2020 09:24
Show Gist options
  • Save vishalkakadiya/6a979c5fe8c0c8b772225848fb9e30bc to your computer and use it in GitHub Desktop.
Save vishalkakadiya/6a979c5fe8c0c8b772225848fb9e30bc to your computer and use it in GitHub Desktop.
Bulk replace strings while rendering page in WordPress
<?php
/**
* Some time we get in situation where we have to replace specific string from whole site,
* Like if you are using WooCommerce for donation purpose and you want to display
* `donation` text in place of `order` text then I have ideal way to do this by below code.
*/
/**
* Filters text with its translation.
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string $translation
*/
function vk_update_strings( $translation, $text, $domain ) {
if ( 'woocommerce' !== $domain ) {
return $translation;
}
// Capital Singular format.
$text = str_replace( 'Order', 'Donation', $text );
// Small Singular format.
$text = str_replace( 'order', 'donation', $text );
return $text;
}
add_filter( 'gettext', 'vk_update_strings', 20, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment