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.

  8. Harish

    Hi, i ma facing issue with this gettext-filter in my WordPress site. I think its compatible issue with my current theme.

    Is this any one facing same issue ?

    Thanks

  9. php online

    You need to fix link of your Youtube video, it is not shown.

    Mixed Content: The page at ‘https://pippinsplugins.com/introduction-gettext-filter-wordpress/’ was loaded over HTTPS, but requested an insecure resource ‘http://www.youtube.com/embed/FvWuvlTYqFQ?feature=oembed’. This request has been blocked; the content must be served over HTTPS.

    • Pippin

      Fixed!

  10. Keith Jones

    I have worked out what the code means. But I am struggling with $domain.

    How do you work out what to use for $domain.

    Using on WP site with Thesis theme.

    Keith

    • Keith Jones

      I believe $domain should be thesis, however appears to work with whatever I put in?

      Keith

    • Pippin

      The domain can be anything. It is intended to indicate the “source” of the text string.

      For example, in our Easy Digital Downloads plugin, we use “easy-digital-downloads” as the domain so that it’s clear the text strings come from EDD.

  11. Keith Jones

    Thank you.

Comments are closed.