Search code examples
phpwordpressline-breaks

are line breaks possible in esc_html_e()?


I am using the following code:

<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are trying to access a restricted page without being logged in to gain access.', 'shapely' ); ?></p>

Which ends up being displayed as follows:

It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are trying to access a restricted page without being logged in to gain access.

What I want to display is this:

It looks like nothing was found at this location.

Maybe try one of the navigation links above or a search?

Or it is possible you are trying to access a restricted page without being logged in to gain access.

So what I want is a way to break the line. I tried <br /> and \n, and neither work. Is there a way to add line breaks in the esc_html_e() function?


Solution

  • First off you can't use \n inside single quotes. Also, line breaks won't be rendered as such on on a webpage. From the looks of things esc_html_e() actually echoes the output so to capture & process it you'd need to do something like this:

    ob_start();
    esc_html_e("Line1\nLine2\nLine3");
    $output = ob_get_contents();
    ob_end_clean();
    echo nl2br($output);
    

    But this seems like an awful lot to go through and probably the wrong way to go about it. If you need to output html you probably don't be using esc_html_e() to begin with. Really hard to say without more context.