Search code examples
phphtmlwordpressgettext

PHP gettext (WordPress Plugin) change the font style


Good Day Everyone,

I am currently developing a WordPress plugin that will be "translatable".

My problem that I have is if I use special font style in my string that is returned I also see the "html" police modification.

For example:

__('This <strong>text</strong> has to be bold', 'my-plugin')

How do I make it that "text" become "Bold" but that I don't see the in the Translation table?


Solution

  • After looking a lot deeper in many documentations I finally came up with the solution.

    In fact, the best practice is not to input any font style or html codes inside a translatable string.

    The proper way is one of the following:

    If you want to change the font style for only 1 word, and that word stays the same for every language, you basically have to remove it from the string and replace it with a gettext placeholder (as follow).

    $string = sprintf( __('This %s has to be bold', 'my-plugin'), '<strong>text</strong>');
    

    If your whole text has to be in a certain font style you have to add the code before outside the string:

    $string = '<h3>' . __('This is a Title', 'my-plugin') . '</h3>'
    

    Of course those should be inside a PHP tag.