$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Woocommerce textarea format ignored|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Woocommerce textarea format ignored

matteradmin8PV0评论

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
&lt;b&gt;row2&lt;/b&gt;&lt;/br&gt;
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
&lt;b&gt;row2&lt;/b&gt;&lt;/br&gt;
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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

The 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 ) );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far