Upload File in My Account Registration Form

How to upload profile picture or any file during WooCommerce My Account Registration?

 

For uploading file from WooCommerce My Account Registration form we need to set form enctype type To multipart/form-data.secondly , we need to add html tags for input type file to upload file. then we need to validate them if the field field is required.Finally after upload file enter media id to user meta.

 

For uploading files may be image or other files in WooCommerce Registration form. We should follow these steps

  1. Make form enctype type To multipart/form-data
  2. We have to add html of input type file in form
  3. Than we have to validate these fields
  4. Than Finally Upload File and save field to User meta

Step 1 : We have to use ‘woocommerce_register_form_tag’ Hook.

 

// Add enctype to form to allow file upload
function AddEnctypeCustomRegistrationForms() {
  echo 'enctype="multipart/form-data"';
}
add_action('woocommerce_register_form_tag','AddEnctypeCustomRegistrationForms');

 

Step 2 : We have to use ‘woocommerce_register_form’ Hook.

 

// Add file input html to register form
add_action('woocommerce_register_form','AddImageField');
function AddImageField() {   
  ?>    
    <p class="form-row validate-required" id="pro_image" data-priority="">
    <label for="pro_image" class="">Image (JPG, PNG, PDF)<abbr class="required" title="required">*</abbr></label>
    <span class="woocommerce-input-wrapper">
      <input type='file' name='pro_image' accept='image/*,.pdf' required>
    </span>
    </p> 
  <?php       
}

 

Step 3: We have to use ‘woocommerce_registration_errors’ Hook.

 

// Validate new fields
function ValidateImageField( $errors, $username, $email ) {
  if ( isset( $_POST['pro_image'] ) && empty( $_POST['pro_image'] ) ) {
    $errors->add('pro_image_error', __( 'Please provide a valid image', 'woocommerce' ) );
  }
  return $errors;
}
add_filter('woocommerce_registration_errors','ValidateImageField',10,3 );

 

Step 4: We have to use ‘user_register’ Hook.

 

// Save new field
function SaveImageField( $customer_id ) {
  if ( isset( $_FILES['pro_image'] ) ) {
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    $attach_id= media_handle_upload('pro_image', 0 );
    if ( is_wp_error( $attach_id) ) {
      update_user_meta( $customer_id,'pro_image', $_FILES['pro_image'] . ": " . $attach_id->get_error_message() );
    } else {
      update_user_meta( $customer_id,'pro_image', $attach_id);
    }
  }
}
add_action('user_register','SaveImageField',1);

 

Related Blogs

Customize Woocommerce Settings Tab

Let’s talk about customizing Woocommerce Admin setting Tabs.To hide a specific Woocommerce setting tab or want to customize the Woocommerce setting tabs, not the entire sub-menu, but just a tab.For just Removing tabs we can use

Jquery Form Validator With User Name Exist User Email Exist

To implement jQuery Form Validator with the specified validation rules, including required, minlength, email, equalTo, custom validation, confirm password, username pattern, email existence in the database in WordPress, and username with no numeric value, you’ll need to combine client-side validation using jQuery Form Validator with server-side validation using PHP and WordPress functions.

How to add custom order status in WooCommerce

To add a custom order status and customize its email template using `register_post_status`, `wc_order_statuses`, `woocommerce_email_actions`, and `woocommerce_email_classes`, you can follow these steps:

How To Limit Search To Only Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.