Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active December 21, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glueckpress/6219706 to your computer and use it in GitHub Desktop.
Save glueckpress/6219706 to your computer and use it in GitHub Desktop.
[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