Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Modify default (not translated) language strings of a plugin. Beware: Might result in performance issues when applied to many strings.
<?php
/**
* Translate text strings in WordPress programmatically.
*
* Assuming plugin intorduces "Foo_Plugin" class, has text domain "foo-plugin".
*
* @param mixed $translated
* @param mixed $original
* @param mixed $domain
* @return string
*/
function gp1410201916( $translated, $original, $domain ) {
/* Only strings from given plugin */
if( ! class_exists( 'Foo_Plugin' ) || 'foo-plugin' !== $domain ) {
return $translated;
}
/* 'Original string' => 'New string' */
$strings = array( 'Wordpress' => 'WordPress' );
if( isset( $strings[ $original ] ) ) {
$translations = &get_translations_for_domain( $domain );
$translated = $translations->translate( $strings[$original] );
}
return $translated;
}
add_filter( 'gettext', 'gp1410201916', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.