How do I add a break line to this php code?

I accidentally started a thread in css/html forum, should have been in the PHP forum. How do I create a newline BEFORE the %s (right after “for:”) in the following code I pulled from woocommerce? Thank you!

echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for: %s.' , 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) ) ;

If the output is html, can’t you put a <br /> in it? I suspect I’m betraying my ancient html coding there, though.

echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for:<br /> %s.' , 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) ) ;

If the output is html, the question was probably in the correct place.

(Note that I know nothing about platforms, so if your function wp_kses_post is one that strips out html, obviously this might not be the solution. )

I tried that. Maybe Im not doing it right?

I’m guessing that because its wrapped in a function called esc_html__ that sticking html tags in is going to be less than effective…

Following the thing’s example though… look at the whole sprintf…

sprintf( esc_html__( 'No shipping options were found for:<br /> %s.' , 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>')

So… if we step the string we know is safe out of the escaper…

sprintf( 'No shipping options were found for:<br />'.esc_html__( '%s.' , 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>')

You might have better luck? (I’ma be honest, i dont know why esc_html__ is around this at all given that its a fixed string that wont have had the sprintf replacement done yet at that point, but hey-ho.)

1 Like

I do not know for what the %s is good at all if it is always filled with woocomerce.

Hard to say without seeing what you tried. Unless it was exactly as I typed, in which case @m_hutley probably has it - the surrounding functions stripping it out.

It worked! Thanks. So, there should be no issues with sprintf vs esc_html? Thanks again!

The "woocommerce" string is a parameter for esc_html__(), not for sprintf(). It appears to be a “domain”, though I don’t know enough (anything) about Wordpress to know what that actually means in this context.

The %s is filled with the formatted destination.

1 Like

Or, stick the break just before the strong:

echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for: %s.' , 'woocommerce' ) . ' ', '<br /><strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) ) ;
1 Like

Thanks! That also worked.

But in that case %s might contain something a user has inputted and so you need to remove html Tags to disable XSS

The destination variable is processed by a separate esc_html() function, before being sent into the main esc_html__() function as the %s contents.

Like i said, I dont really see a point to the inner esc_html in there - esc_html(‘%s’,‘anything’) is going to return “%s”, so… shrug

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.