Home > Software engineering >  how to create line breaks using php
how to create line breaks using php

Time:11-16

I am not very good with PHP but am trying to add line breaks to some woocommerce text. I have tried:

add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
echo nl2br(   $new_str = $str .  " 

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x
");
    return $new_str;
}

This gives me the line breaks but also shows the text a 2nd time without line breaks:

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x Thank you. Your order has been received. You will shortly receive a confirmation email. We will email you again once your order has been dispatched. With best wishes – and happy styling, Wendy & Emma x

How do I get it to show the text once only with line breaks? Thanks

CodePudding user response:

You are using echo when setting the value and only displaying the value with line breaks (using nl2br()).

It's the echo which is displaying the value the first time. The second time is the returned value from the function.

Instead you just need to return the new value...

function woo_change_order_received_text( $str, $order ) {
    return nl2br( $str .  " 

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x
");
}
  • Related