skip to Main Content

I am coding a simple plugin and I have a line of code:

echo "<p style="color:red;">Please enter a valid email address!</p>";

But in order to localize / internationalize the plugin, I need to use the function like:

_e('Please enter a valid email address!','mytextdomain')

Then may I know how do I style the text inside the _e() function?

Any help will be appreciated, thank you very much!

Regards
KC

2

Answers


  1. You can use sprintf. check the below code.

    The Better way.

    $html = sprintf(
        '<p>%s</p>',
        __( 'Please enter a valid email address! ', 'mytextdomain' ),
    );
    

    OR you can do by this way

    echo '<p style="color:red;">'.__( 'Please enter a valid email address! ', 'mytextdomain' ).'</p>";
    

    OR

    __( '<p style="color:red;">Please enter a valid email address!</p>', 'mytextdomain' );
    

    USEFUL LINKS

    Login or Signup to reply.
  2. As per the WordPress standard you can not use the html tag inside the _e() function.
    Here I have shared the WordPress official link for your reference:
    URL: https://developer.wordpress.org/reference/functions/_e/

    As you can see in this function 1st parameter is string $text and 2nd paramenter string $domain = ‘default’.

    Here I have shared the example of code which you can use.

    echo '<p style="color:red;">' . __( 'Please enter a valid email address!', 'mytextdomain' ) . '</p>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search