I am tired...

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 :

22 Comments

Add a Comment
  1. Very useful, thanks for this tip.

  2. 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.

  3. 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.

  4. 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. Those particular phrases are translatable. You should be able to change them by using the functions mentioned on this post.

      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?

        1. Frontier Options -> Display -> Footer Text

          For questions unrelated to this post, comment on the Frontier Page or on the forum.

  5. 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.

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

  7. Thanks a lot for this help! It works very well :)

  8. Hi Ron

    I tried to do the above (first one, adding to functions.php) to change the word “Comments” to the Danish word “Kommentarer”, but it does not seem to work, as you can see here: http://www.cefas.dk/artikler/.

    Do you know what I am doing wrong or need to do to make it work?

    Regards and thx in advance!

    1. Upon page load your text is shown as “Kommentarer” but it is then quickly changed to “Comments”. It is probably your Disqus plugin that is responsible. You can go to Disqus moderate page then to the settings tab where you can change the comments wordings.

  9. I noticed that too. However, the Disqus plugin already is set to Danish and works well elsewhere on the site. This is why I thought the specific “Comments” word in question did not have anything to do with Disqus. I will, however, look further into the Disqus settings.

    Thx for your time Ron!

  10. You were absolutely right, as usual, Ron. For others reading this, the language and/or wording of “Comments” can be changed in the Disqus settings.

  11. Nice.

    But how do You translate variables like:

    $category_list = 'All products';

    We need to make that ‘All Products’ multilingual with WPML.

    Thanks in advance. Can you reply to my email? :)
    Martin

    1. That text isn’t translatable so your only option is to edit the file.
      http://ronangelo.com/change-or-translate-text-on-a-wordpress-theme/#comment-1733

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 © 2012 - 2014 Frontier Theme
css.php