WordPress translations strings with variables code snippet

A quick code snippet to remind us how to do translations with variables in WP.

I kept forgetting how to do this, so I’ve enshrined in a blog post, as a code snippet.

If you have a string translation that contains a variable and you want to translate that whole string you can use:

The print_f PHP function allows you to pass the contents of a variable in this case $persons_name to the place holder %s. The __(); function, is one of WordPress’ translation functions, it requires the first parameter of the base language string (what you want to translate) and the second parameter of your text domain, the ‘namespace’ if you like of all your string translations.

For more translation function including how to add a contextual note for you translator refer to the WP documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *