Overwrite English Labels in Modern Tribe Events Calendar Plugin

January 29th, 2014 - Posted by Steve Marks to PHP, Web Development.

I recently installed and setup the Event Calendar plugin from Modern Tribe on a WordPress site. It worked great out of the box and was so easy to customize through use of the templating system that it comes with.

I could get it looking exactly how the client wanted, with the exception of a few small bits and pieces; There were a few labels output by the plugin what needed to be re-worded slightly.

An example of this was one of the labels in the search dropdown:

Modern Tribe Event Labels - Before and After

Note: I’m using the Event Calendar PRO add-on in this example.

Notice how we’ve changed the ‘Near‘ label and ‘Location‘ placeholder to be postcode orientated? We found that this location search doesn’t work great here in the UK due to lack of region biasing so, as a result, we wanted to push users towards searching for postcodes instead which gives much better results.

Now, these bits of text are generated from within the plugin code and look a little something like so:

__('Near', 'tribe-events-calendar-pro');

If you haven’t seen the double underscore syntax (__()) being used above before, it basically allows a plugin to translate it’s code into different languages. Inside the plugin folder there is a subfolder called ‘lang‘ which contains various PO files, each one containing the different translations.

Now, we could simply open the English translation file here and modify the labels, but this presents an issue should the plugin ever get updated. The language files could get overwritten and we’d lose our changes. This isn’t ideal, especially if you’re like me and are always keeping your plugins up-to-date.

The better solution that I found was to perform the change in my theme functions.php file. I just needed a way to intercept this translation and perform my own. Fortunately I discovered a filter called gettext which is applied to text being translated.

After a bit of tinkering I came up with the code below to change the labels in question:

function change_tribe_event_labels( $translated_text, $text, $domain ) 
    // Check the domain and only perform it for this case.
    // If another plugin was to translate the same word we
    // would not want to change it then
    if ( $domain == 'tribe-events-calendar' || $domain == 'tribe-events-calendar-pro' )
        switch ( $text )
            case 'Near' :
                $translated_text = 'Near Postcode';

            case 'Location' :
                $translated_text = 'Postcode';
    return $translated_text;
add_filter( 'gettext', 'change_tribe_event_labels', 20, 3 );
This entry was posted on Wednesday, January 29th, 2014 at 10:26 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first