How to create translation ready shortcodes

Remi | March 21, 2013 | in Shortcodes

I use many themes and plugins from other talented authors, but most of them are not French like me. That’s why each time i use a plugin or a theme from a non French developer, i have the same problem: shortcodes. Well, it’s not really a big problem for me because i speak English, but what about my French clients that don’t speak English at all? Imagine: they’re writing a post in French into their French version of the administration, and suddenly, when the time comes to insert a shortcode they have to insert it in English… Wow! That makes me feel bad. Their texts would then be similar to this:

“Il y a actuellement en ligne: [online_users] utilisateurs”.

Which means :

“At the moment [online_users] connected users”.

Imagine if you were having the same thing in English:

“At the moment [utilisateurs_connectes] connected users”.

I’m sorry, but writing the shortcode in another language isn’t user friendly at all because it’s not logic. So i decided to show you how to create a user friendly shortcode that can be used in any language. I’m talking about French, but that’s the same thing with every language!

Step 1

The first step is to create a really simple plugin. Create a new folder under wp-content/plugins and name it: “translation-ready-shortcodes”. Then open it and create a new “index.php” file. Place this code :

This code simply creates the plugin, nothing more.

Step 2

We then need to load language files using load_plugin_textdomain(). If you want to use this method with a theme use load_theme_textdomain() instead. So, add this code:

Step 3

Here we are! The interesting part! In this step we need to register the shortcode. In my sample, i’m going to use this shortcode : [link to=””]WordPress[/link]. The aim is to echo a simple link.

The basic code to do this is:

But the problem here, is that if a French guy uses this shortcode he will have to write “link” instead of “lien”, the French translation. So, what i want to do is to give this poor French guy the ability to write: [lien vers=””]WordPress[/lien]. That would be much better.

To do so, i need to modify on the fly the shortcode itself (the tag), and the shortcode attributes (in this case, the word “to”). For the first one it’s easy, we simply need to use the __() function, but for the second one it’s more complex. We need to use a variable having a variable name. The code becomes:

That’s it! I mean for the code that’s it, now if you have to language file you can try to switch from one language to another… that works! It means that a English guy will use:

[link to=””]WordPress[/link]

and a French guy will use:

[lien vers=””]WordPress[/lien]

while an Italian guy will do:

[collegamento a=””]WordPress[/collegamento]

Isn’t it great?

Download the code

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!


  1. FxB

    Thanks i added this piece (or i should write peace) of code to my translation snippet reminder. I will hunt GitHub repo of WordPress extensions with it.

  2. thierrypigot

    Thank you for this article. In fact, I use this trick for several years. Strings to translate or adapt the parameterization based on the language.


  3. Steven Zahm

    I like the idea and I was going to implement it in my plugin but this could break the new shortcode_atts_{$shortcode} filter in WP 3.6, read here:

    Basically translating the shortcode tag, another plugin couldn’t reliable hook into this filter.


    Although not impossible to work around, using the $tag option in the shortcode callback would be more difficult. Here’s a link to a tutorial that simplifies shortcode by using the $tag option.


    1. Remi Post author

      Hi Steven. I also read these two excellent posts, and to be honest i haven’t tested the new shortcode_atts_{$shortcode} filter in the beta, so for now i don’t know how it can interact with what i describe in this article. But i will definitely give it a try!

  4. SEO Translator

    As an IT professional, I must say that I like the idea and your code, but as a translator with 36 years of experience I can only smile – any real professional translator worth his salt will be able to spot short codes and will obviously NOT translate them!

  5. alexandreb

    Hi Rémi,

    I managed to make it work but I had to wrap my add_shortcode functions into a function and hook it to init.

    I hope that snippet could help someone stuck ;)

    function coco_shortcodes_register(){
    add_shortcode(__(‘cocorico_column’, ‘cocoshortcodes’), ‘coco_shortcodes_column’);
    add_action( ‘init’, ‘coco_shortcodes_register’);

Leave a reply