Rule #1 – This is my blog.

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 change_translate_text( $translated_text ) {
	if ( $translated_text == 'Old Text' ) {
		$translated_text = 'New Translation';
	}
	return $translated_text;
}
add_filter( 'gettext', 'change_translate_text', 20 );

To change or translate multiple strings, add this code on your functions.php. If needed, follow the code pattern to add more.

function change_translate_text( $translated_text ) {
	switch ( $translated_text ) {
		case 'Old Text 1' :
			$translated_text = 'New Translation 1';
			break;

		case 'Old Text 2' :
			$translated_text = 'New Translation 2';
			break;

		case 'Old Text 3' :
			$translated_text = 'New Translation 3';
			break;
	}
	return $translated_text;
}
add_filter( 'gettext', 'change_translate_text', 20 );

Here’s another way to do it. Using arrays can be a more efficient way to change multiple text. This one isn’t case-sensitive.

function change_translate_text( $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', 'change_translate_text', 20 );
Don’t use these codes together. Just pick one that suits your needs. It would cause a PHP error as these functions have the same name. There would be no need to use them all together anyway.
Share This Post :

16 Comments

Add a Comment
  1. You don’t have to use switch() function to translate multiple strings. Just put all strings in an array.
    Here is an example,

    function change_translate_text( $translated, $original, $domain ) {
    	$strings = array(
    		'String 1' => 'New String 1',
    		'String 2' => 'New String 2',
    		'String 3' => 'New String 3'
    	);
    	if ( ! empty( $strings[$original] ) ) {
    		$translations = &get_translations_for_domain( $domain );
    		$translated = $translations->translate( $strings[$original] );
    	}
    	return $translated;
    }
    add_filter( 'gettext', 'change_translate_text', 10, 3 );
    1. Hi Prassana, thanks for sharing. I’ve added a new snippet that uses an array. Your exact example gives me an error saying “Strict standards: Only variables should be assigned by reference…” Though this might be the case only when debug is set to true.

  2. Hi,
    I tried adding this to my functions.php,
    translated a couple of words, I wrote everything just like it was in the
    theme but nothing changes.
    What i’m doing wrong?

    Thanks.

    1. You can only use this function with words that are translatable, meaning those that are wrapped in these functions ( __(), _e() ). Not all text are translatable it will depend on the theme. What words were you trying to change?

      1. For instance I tried to translate “The Search Box”.
        My site is in hebrew, but here and there you can find words that are written in English.
        What I need to do in order to translate them?

        1. The ones that are displayed right now are the default widgets which means you have not selected any widgets on the widget page.

          Go to Dashboard -> Appearance -> Widgets then add those same widgets on the Sidebar – Right area. You can add whatever title you like.

  3. Thank you, it works.

    Now, when I try to look for something and there are no results,
    the message is written in english.

    Also, when there are results, it says “Search Results for”.

    Whan I need to do to change all of these?

      1. Thank you, works like a charm!

        Sorry for the amount of the questions, still kinda new to this.
        There is this black\gray bar as a footer, the one that says
        “ronangelo © 2014″ and links to Frontier theme,
        I tried to look for a way to add links or text but couldn’t
        find how to do this.
        How I do this?

  4. Everything works like a charm,
    The only thing I’m not able to translate is the headline
    that is written in the tab of the browser.

    If you go into my site and use the search box (right column, first widget) and you search for something, it will say
    ” You searched for XYZ ” on the browser tab.
    How can I translate it? The method in this post dosen’t work on that.

    1. The <title> “You Searched For” is outputted inside the <head> by WordPress itself and not the theme. It should automatically be translated if it’s done properly on the language file you’re using. Check here http://he.wordpress.org/ I’m guessing that’s where you downloaded the WordPress-Language files.

      –Edit–
      I just downloaded the hebrew language files (.mo|.po) and the title search text is properly translated. You’re probably using an outdated file. You could also try disabling your yoast plugin as that is responsible for modifying the text on the <title> and is most likely the culprit.

  5. VERY useful tip, thanks! It saved me having to override an entire function just to change some wording. Will be using this again!

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>

ronangelo © 2014 Frontier Theme
css.php