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
|
1 Answer
Reset to default 0The 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
update_post_meta()
, which$product->update_meta_data()
uses internally, shouldn't require any special treatment in this regard because it useswp_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:10update_meta_data()
(which callsadd_meta_data()
) , i checked the API docs form WC and those do not callwp_unslash()
, i'll try wrappingwp_unslash
around myupdate_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:42update_post_meta()
, which callsupdate_metadata()
which callswp_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