Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active December 14, 2015 21:33
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/224948d6680ff574d320 to your computer and use it in GitHub Desktop.
Save glueckpress/224948d6680ff574d320 to your computer and use it in GitHub Desktop.
[WordPress] Mini example plugin. Modify default (not translated) language strings of a plugin. Beware: Might result in performance issues when applied to many strings.
<?php
/**
* Plugin Name: Custom Text Strings for Foo Plugin
* Description: Modify default (not translated) language strings of a plugin. Beware: Might result in performance issues when applied to many strings.
* Version: 2014.10
* Author: Caspar Hübinger
* Author URI: http://glueckpress.com/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Modify default text strings of Foo plugin.
* 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