Change or Translate Text on a WordPress Theme

You may want to replace a line or two of text on your WordPress Theme. You can easily change or translate a text by adding this code on your WordPress Functions. If what you’re after is to actually translate the entire theme and not just a couple of words then read this post instead: How to add Language Files on a WordPress Child Theme

This code makes use of the gettext function. Replace Old Text with the string you want to change. This is case-sensitive so type it exactly as you see it on your WordPress Theme. Replace New Translation with your new text replacement. You can leave it blank if you want to remove the original text.

To change or translate a single string, add this code on your functions.php.

function ra_change_translate_text( $translated_text ) {
	if ( $translated_text == 'Old Text' ) {
		$translated_text = 'New Translation';
	}
	return $translated_text;
}
add_filter( 'gettext', 'ra_change_translate_text', 20 );

Using arrays can be an efficient way to change multiple text. This one isn’t case-sensitive. To change or translate multiple strings, add this code on your functions.php. If needed, follow the code pattern to add more.

function ra_change_translate_text_multiple( $translated ) {
	$text = array(
		'Old Text 1' => 'New Translation 1',
		'Old Text 2' => 'New Translation 2',
		'Old Text 3' => 'New Translation 3',
	);
	$translated = str_ireplace(  array_keys($text),  $text,  $translated );
	return $translated;
}
add_filter( 'gettext', 'ra_change_translate_text_multiple', 20 );
Share Post :

31 Comments

Add a Comment
  1. Hi,

    I´ m using the latest wordpress version but I cannot use the tranlsation. I´ m getting following error:

    FastCGI sent in stderr: “PHP message: PHP Fatal error: Call to undefined function add_filter()

    1. There’s probably some stray/missing comma(,) or semi-colon(;) on your code.

  2. I just wanted to thank you for this awesome code. you know how long it took me a code that actually works?

Leave a Reply

Please write your comments in english. I delete anything that even remotely resembles spam.

Note: When posting code, enclose it in pre and code tags.
e.g. <pre><code> Add code here </code></pre>