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:
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'; break; case 'Location' : $translated_text = 'Postcode'; break; } } return $translated_text; } add_filter( 'gettext', 'change_tribe_event_labels', 20, 3 );