The gettext filter gives us a really easy way to change text anywhere in our WordPress install, including plugins and themes, without ever changing any of the original code. It is a great way to easily customize labels, headings, button text, and any other text we wish to change.

Like most filters in WordPress, the gettext filter is exceptionally simple to use, and the main purpose of this filter is to allow us to change text in WordPress core, WordPress plugins, and WordPress themes.

For example, let’s say a plugin has a button that says “Add to Cart”, but you would rather it say “Purchase”; the gettext filter will make changing this button label super simple. This example assumes that the plugin does not already have a method for changing the label.

A very important thing to note about the gettext filter, however, is that it only works for text strings that are localized and ready for translation.

Note, as pointed out by Otto, you should take care when using the gettext filter, as it runs on every single string in WordPress core.

Have you ever used the gettext filter before? have problems with it? Found it to be awesome, or terribly confusing? Let me and everyone else know in the comments.

  1. Gregg FranklinGregg

    Thank you!

  2. Bruce

    Great tip, especially for us semi-neophyte WP coders 🙂

    Thanks Pippin!

  3. Per Soderlind

    I use the gettext filter to remove the annoying “Comments are closed.”:


    add_filter('gettext', 'ps_remove_comments_are_closed', 20, 3);

    function ps_remove_comments_are_closed($translated_text, $untranslated_text, $domain) {
    if ( $untranslated_text == 'Comments are closed.' ) {
    return '';
    }
    return $translated_text;
    }

    The plugin is available in the WP Plugin Repository: http://wordpress.org/extend/plugins/remove-comments-are-closed/

  4. GeorgeInRaleigh

    Completely lost me. I guess I’ll just need to deal with it as is. Thanks.

    • Pippin

      If you let me know which part you’re confused by I’ll be happy to help clarify it.

  5. Mel

    Hello…I tried to use it with Theme my login plugin…and it’s not working. I want to change the label “Lost Password” to “Forgot your password?”.

    Here’s my code :

    add_filter( ‘gettext’, ‘change_label_names’, 20, 3 );

    function change_label_names( $translated_text, $text, $domain ) {

    if ( $translated_text == “Lost Password” ) {
    $translated_text = “Forgot your password?”;
    }
    }
    return $translated_text;

    Maybe it’s because of this “change_label_names” … not sure how to call this function.

    Can you help me with that ?

    Thanks !

    • Pippin

      Use this:

      1
      2
      3
      4
      5
      6
      7
      8
      
      add_filter( 'gettext', 'change_label_names', 20, 3 );
      function change_label_names( $translated_text, $text, $domain ) {
       
      	if ( $translated_text == 'Lost Password' ) {
      		$translated_text = 'Forgot your password?';
      	}
      	return $translated_text;
      }
    • Per Soderlind

      If the text in your template is “Lost Password” as in _e(“Lost Password”), you should test on the second parameter, $text. This contains the untranslated text.

      [php]
      add_filter( ‘gettext’, ‘change_label_names’, 20, 3 );

      function change_label_names( $translated_text, $untranslated_text, $domain ) {

      if ( $untranslated_text == “Lost Password” ) {
      $translated_text = “Forgot your password?”;
      }
      return $translated_text;
      }
      [/php]

  6. Vincy

    This could lead to a performance problem and so use it cautiously.

  7. Chris

    Pippin,

    Maybe I’m totally lost. But after 4 days of searching, and a few plugins, I was finally able to change one string (error message in Gravity Forms) using your example in the comments.

    Thanks so much.

Leave a Reply

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match