Sanitize custom validation field data in WooCommerce

Following the instructions for setting up WooCommerce validation fields, follow these steps:
Setting up validation fields using actions and filters

I added a custom field to the woocommerce validation page via functions.php .

I'm worried if I need to sanitize user login for this custom field?

I think that he does not need to be disinfected, since he went to billing fields, as in: $ fields ['billing'], is that right?

If not, how do I sanitize this custom field?

Creating this custom field means that text strings (Latin) and integers are combined with a maximum length of 50.

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {

//Adding custom text field  
 $fields['billing']['billing_username'] = array(
'type' => 'text',
'label'     => __('Your Username', 'woocommerce'),
'placeholder'   => _x('', 'placeholder', 'woocommerce'),
'required'  => true,
'class'     => array('form-row-first'),
'clear'     => true
 );

 return $fields;
}
+4
1

, , :

/**
 * Update the order meta with field value
 */
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_field_name'] ) ) {
        update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}

, Woocommerce.

: yes ( )

, sanitize_text_field() WordPress update_post_meta() ...

, , ...

+5

Source: https://habr.com/ru/post/1668618/


All Articles