Add birthday field to WooCommerce my account and admin user page Add birthday field to WooCommerce my account and admin user page wordpress wordpress

Add birthday field to WooCommerce my account and admin user page


The following code will add (and save) a custom birthday field to

  • My account - edit account
  • Admin user page - profile
// Add field - my accountfunction action_woocommerce_edit_account_form() {       woocommerce_form_field( 'birthday_field', array(        'type'        => 'date',        'label'       => __( 'My Birth Date', 'woocommerce' ),        'placeholder' => __( 'Date of Birth', 'woocommerce' ),        'required'    => true,    ), get_user_meta( get_current_user_id(), 'birthday_field', true ));}add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );// Validate - my accountfunction action_woocommerce_save_account_details_errors( $args ){    if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {        $args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );    }}add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );// Save - my accountfunction action_woocommerce_save_account_details( $user_id ) {      if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {        update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );    }}add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );// Add field - adminfunction add_user_birtday_field( $user ) {    ?>        <h3><?php _e('Birthday','woocommerce' ); ?></h3>        <table class="form-table">            <tr>                <th><label for="birthday_field"><?php _e( 'Date of Birth', 'woocommerce' ); ?></label></th>                <td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>            </tr>        </table>        <br />    <?php}add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );// Save field - adminfunction save_user_birtday_field( $user_id ) {    if( ! empty($_POST['birthday_field']) ) {        update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );    }}add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );