1

I've been trying to figure this out for a while and I can't seem to figure it out using <br> or \n but not sure where to place it. I've got this php line for email results and I want to place a line break in between the sentences. No matter what I try it crashes the site.

Here is the line:

'post_alert_user' => esc_html__('Thank you for your submission at ', 'usp-pro') .$user['admin_name'] . esc_html__('! If submissions require approval, you'll receive an email once it's approved.', 'usp-pro'),

So basically, I want the email results to look like this instead of running all in one line:

Thank you for your submission at HIV Heros!

If submissions require approval, you'll receive an email once it's approved.

3
  • when you say it crashes the site, what's the PHP error message? What is the change you made to add the newline that caused the crash?
    – Tom J Nowell
    Commented Feb 28 at 18:56
  • Note that it looks like you're using a 3rd party plugin usp-pro, questions about 3rd party plugins are offtopic here, and general answers might not work for you as plugins work in different ways and there'd be no way for someone to check
    – Tom J Nowell
    Commented Feb 28 at 18:57
  • 1
    You've got two 's (single quotes) in a string wrapped in ' (single quotes); either change the inner 's to \' or else wrap the string in " (double quotes).
    – Pat J
    Commented Feb 28 at 19:10

2 Answers 2

0

You are using single quotation in single quotation which is not possible, use one of the below approch,

Approch 1 (' \' '):

'post_alert_user' => esc_html__('Thank you for your submission at ', 'usp-pro') .$user['admin_name'] . esc_html__('! If submissions require approval, you\'ll receive an email once it\'s approved.', 'usp-pro'),

Approch 2 (" ' "):

'post_alert_user' => esc_html__('Thank you for your submission at ', 'usp-pro') .$user['admin_name'] . esc_html__("! If submissions require approval, you'll receive an email once it's approved.", 'usp-pro'),
0

You have few issues with your string trnalation.

  1. Any of esc_html* function will escape html tabs, so you can't directly use line brake <br> and \n will be ignored too if it's a html email.
  2. Concatenate string will brake translation (E.G. to RTL) in different language.
  3. And an unescaped single quote (') symbol. You can wrap your string with double quote (") or use escape character (E.G. 'you\'ll receive').

To fix everything it's recommended to use printf() or sprintf() function with appropriate placeholders and translators comment.

You can also use wp_kses() or wp_kses_post() to output strings with html tags. Keep in mind kses functions are slow.

Examples:

$data = [
    // ...
    'post_alert_user' => sprintf(
        /* translators: 1. Admin Name, 2. Line brake (br tag) */
        esc_html__('Thank you for your submission at %1$s!%2$sIf submissions require approval, you\'ll receive an email once it\'s approved.', 'usp-pro'),
        esc_html( $user['admin_name'] ),
        '<br>'
    ),
    // ...
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.