post meta - Saving Custom Field that includes Quotation marks

I have created a custom field in woocommerce that needs to be saved as 10"x10"x10". This is a measurement

I have created a custom field in woocommerce that needs to be saved as 10"x10"x10". This is a measurement and needs to have the double quotation marks. I will display this in a custom tab on the product page.

Upon saving it adds escaping slashes to the output like this: 10\"x10\"x10\"

I've tried htmlspecialchars, htmlentities & esc_html functions but nothing worked.

I did use str_replace(\\\, '', $value) and that did achieve what i need, but pretty sure thats not what to do

any help would be appreciated, thanks!

This is how i am creating the input, on the general section of the WC product page in the admin:

function create_dimension_field() {
    $args = array(
        'id'            => 'overall_dims',
        'label'         => 'Overall Dimensions',
        'class'         => 'custom-field',
        'desc_tip'      => true,
        'description'   => 'Enter the overall dimensions here', 
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'create_dimension_field' );


function save_dims_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $value = isset( $_POST['overall_dims'] ) ? $_POST['overall_dims'] : '';
    $product->update_meta_data( 'overall_dims',  sanitize_text_field( $value ));
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'save_dims_field' );

I have created a custom field in woocommerce that needs to be saved as 10"x10"x10". This is a measurement and needs to have the double quotation marks. I will display this in a custom tab on the product page.

Upon saving it adds escaping slashes to the output like this: 10\"x10\"x10\"

I've tried htmlspecialchars, htmlentities & esc_html functions but nothing worked.

I did use str_replace(\\\, '', $value) and that did achieve what i need, but pretty sure thats not what to do

any help would be appreciated, thanks!

This is how i am creating the input, on the general section of the WC product page in the admin:

function create_dimension_field() {
    $args = array(
        'id'            => 'overall_dims',
        'label'         => 'Overall Dimensions',
        'class'         => 'custom-field',
        'desc_tip'      => true,
        'description'   => 'Enter the overall dimensions here', 
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'create_dimension_field' );


function save_dims_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $value = isset( $_POST['overall_dims'] ) ? $_POST['overall_dims'] : '';
    $product->update_meta_data( 'overall_dims',  sanitize_text_field( $value ));
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'save_dims_field' );
Share Improve this question asked Aug 6, 2019 at 20:55 colbyalbocolbyalbo 861 silver badge9 bronze badges 4
  • As far as I was aware, update_post_meta(), which $product->update_meta_data() uses internally, shouldn't require any special treatment in this regard because it uses wp_unslash() before saving the data. Where are you seeing the slashes? In the input field after saving? Or on the front end? Or both? – Jacob Peattie Commented Aug 7, 2019 at 12:10
  • Thanks, I am using the WooCommerce API funcitons , update_meta_data() (which calls add_meta_data()) , i checked the API docs form WC and those do not call wp_unslash() , i'll try wrapping wp_unslash around my update_meta_data() call and see if that works or maybe try just using `update_post_meta' Thanks, for pointing me in that direction! – colbyalbo Commented Aug 7, 2019 at 12:42
  • Internally the WooCommerce functions use update_post_meta(), which calls update_metadata() which calls wp_unslash(). So you shouldn't need to do it again. You didn't answer my question about where the error is though. – Jacob Peattie Commented Aug 7, 2019 at 13:23
  • sorry, i'm seeing the slashes in the input fields in the admin, and the front end. thanks – colbyalbo Commented Aug 7, 2019 at 14:08
Add a comment  | 

1 Answer 1

Reset to default 0

The correct symbol for abbreviating feet and inches is actually "Prime" and "Double Prime" respectively.

You can read about it more here.

I'm not sure if this site will change the character but is Prime and is Double Prime. If you can't copy/paste them from there, you could copy/paste them from the link above.

The link above also lists the Alphanumeric and Unicode values which are ′and ′; for Prime, and ″ and ″ respectively. However, I don't believe these will output properly if put them in the custom field. So, it's probably best to just copy/paste the actual character (or use the keyboard shortcut).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263788a4619332.html

相关推荐

  • post meta - Saving Custom Field that includes Quotation marks

    I have created a custom field in woocommerce that needs to be saved as 10"x10"x10". This is a measurement

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信