Translate or replace text in WordPress via functions.php

You may want to translate or change some text on your WordPress site but you don’t want to mess around with string translations or editing the PO/MO files. Luckily there is an alternative way to do this. Quick and easy. All you have to do is add the code below in the functions.php file of your child theme. Keep in mind that this only works with translatable text that is being used inside __() or _e() functions.

The code below makes use of the gettext filter. Replace Old Text with the text you want to replace. This is case sensitive so type it exactly as you see it on your website. Replace New Text with your replacement text or leave it blank if you want to remove the original text.

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

You can also use this in an array() for example:

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

Thats it!

Support 🐶

If you found this article helpful, got a question or spotted an error/typo... Do well to leave your feedback in the comment section or help spread it by sharing this article. If you're feeling generous (and I hope you do) you can definitely help me by getting me a cup of coffee ☕.


You may like these too!

12 Comments

  • Oh my goodness! Impressive article dude! Thanks, However I am experiencing troubles with your RSS.
    I don’t know why I can’t join it. Is there anyone else having the same RSS issues?
    Anyone that knows the answer can you kindly respond? Thanx!!

    Reply
  • I am actually grateful to the owner of this site who has shared this
    fantastic post at at this time.

    Reply
  • Sory, but it doesnt work for me also.
    My code:
    function change_translate_text( $translated_text ) {
    if ( ‘CONTINUE READING’ === $translated_text ) {
    $translated_text = ‘Kontynuuj czytanie’;
    }
    return $translated_text;
    }
    add_filter( ‘gettext’, ‘change_translate_text’, 20 );

    https://ludzkieseo.pl/blog/

    Reply
    • Hi Jacek,

      Thanks for your feedback! Please note that this only works if text is wrapped with __(); or _e(); function. Can you check if this is the case?

      Please also note that gettext() is case sensitive. If I inspect your button I see it’s written as: Continue reading and styled with CSS to make it uppercase.

      Reply
  • Very nice script, thank you,
    is there a way to use a condition, like when the user change the language on the site then the script runs
    example, it says Old Text 1 and when switching language it becomes New Text 1

    Reply
    • Hi So,

      That’s a very good question! I havent tried that one before. I assume your using WPML to change language?

      Maybe something like this would work:


      function change_translate_text( $translated_text ) {
      if ( 'Old Text' === $translated_text ) {
      if ( ICL_LANGUAGE_CODE === 'fr' ) {
      $translated_text = 'FR Text!';
      } elseif ( ICL_LANGUAGE_CODE === 'nl' ) {
      $translated_text = 'NL Text!';
      }
      }
      return $translated_text;
      }
      add_filter( 'gettext', 'change_translate_text', 20 );

      Reply
  • You genius!

    I’ve been wasting hours using Loco plugin but your first function fixed it for me straight away.

    Reply
    • Hi Tony!
      What approach isn’t working? Let me know so I can check with you.

      Reply

Leave a Reply

Your email address will not be published.