I used the code posted by ClemC in Woocommerce add extra field to variation product with a textarea instead of a text_input. I succeeded to show my textarea under wooocommerce-default "variation description" textarea. I also succeeded to show my textarea information in the product description tab. However, the information does not keep their format (the html format is taken as text and the breaklineas are ignored).
For example, if I fill my textarea with:
row1
<b>row2</b></br>
row3
the next is shown in the product description tab:
row1 <b>row2</b></br> row3
If I go back to my textarea in the product description after updating, the information appears modified to:
row1
<b>row2</b></br>
row3
I will appreciate any help in order to keep the format in my textarea, as happens if I use the wooocommerce-default "variation description" textarea.
Change part of my code to:
function save_variation_settings_fields( $variation_id, $loop ) {
$text_field = $_POST['my_text_field'][ $loop ];
if ( ! empty( $text_field )) {
if ( ! current_user_can( 'unfiltered_html' ) ) {
$text_field = wp_kses_post( $text_field );}
update_post_meta( $variation_id, 'my_text_field', $text_field ); }}
function load_variation_settings_fields( $variation ) {
$variation['my_text_field'] = wpautop( get_post_meta( $variation[ 'variation_id' ], 'my_text_field', true ) );
return $variation; }
And now everything works like a charm. Thanks a lot Jacob!
I used the code posted by ClemC in Woocommerce add extra field to variation product with a textarea instead of a text_input. I succeeded to show my textarea under wooocommerce-default "variation description" textarea. I also succeeded to show my textarea information in the product description tab. However, the information does not keep their format (the html format is taken as text and the breaklineas are ignored).
For example, if I fill my textarea with:
row1
<b>row2</b></br>
row3
the next is shown in the product description tab:
row1 <b>row2</b></br> row3
If I go back to my textarea in the product description after updating, the information appears modified to:
row1
<b>row2</b></br>
row3
I will appreciate any help in order to keep the format in my textarea, as happens if I use the wooocommerce-default "variation description" textarea.
Change part of my code to:
function save_variation_settings_fields( $variation_id, $loop ) {
$text_field = $_POST['my_text_field'][ $loop ];
if ( ! empty( $text_field )) {
if ( ! current_user_can( 'unfiltered_html' ) ) {
$text_field = wp_kses_post( $text_field );}
update_post_meta( $variation_id, 'my_text_field', $text_field ); }}
function load_variation_settings_fields( $variation ) {
$variation['my_text_field'] = wpautop( get_post_meta( $variation[ 'variation_id' ], 'my_text_field', true ) );
return $variation; }
And now everything works like a charm. Thanks a lot Jacob!
Share Improve this question edited Mar 5, 2019 at 15:36 Roasty asked Mar 5, 2019 at 13:00 RoastyRoasty 52 bronze badges1 Answer
Reset to default 0The answer you've based your code off uses esc_attr()
incorrectly on this line:
update_post_meta( $variation_id, 'my_text_field', esc_attr( $text_field ));
esc_attr()
is for escaping a value so that it can be placed in an HTML attribute on output. This is a completely incorrect use of it, and it's what is converting your <
and >
characters. Simply remove it to stop this happening:
update_post_meta( $variation_id, 'my_text_field', $text_field );
Just be aware that this allows anyone who can edit this product to add any HTML they'd like. If you want the textarea to respect the normal permissions rules regarding HTML tags in content, check the user's permissions and remove disallowed tags with wp_kses_post()
if they're not allowed to add all tags:
if ( ! current_user_can( 'unfiltered_html' ) ) {
$text_field = wp_kses_post( $text_field );
}
update_post_meta( $variation_id, 'my_text_field', $text_field );
That will ensure that HTML tags will work inside your textarea, but it won't preserve line breaks. If you also want to preserve line breaks and paragraphs, the way the classic WordPress editor's Text view does, then you need to pass the value through wpautop()
on output:
echo wpautop( get_post_meta( $variation[ 'variation_id' ], 'my_text_field', true ) );