Adding custom fields to the vendor registration form

Updated and tested with WooCommerce 4.9.2 and Product Vendors 2.1.47.

With Product Vendors 2.0 being released a while ago, the developers added many new features, including the vendor registration form which you requested so many times.

By using the shortcode [wcpv_registration] in a page, the user will see a form to register an account and a vendor account, if not logged in, or only the fields to register the vendor account if they are already logged in.

The form anyway, could not be enough for someone. You may want to add more fields to the vendor, like their social profiles. Like always, there’s a way, with some custom code, to do it.

The example code adds two new fields to the Vendor taxonomy and the registration form to allow vendors to specify their Facebook and Twitter URLs.

Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

/**
* Register term fields
*/
add_action( 'init', 'register_vendor_custom_fields' );
function register_vendor_custom_fields() {
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_add_form_fields', 'add_vendor_custom_fields' );
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_edit_form_fields', 'edit_vendor_custom_fields', 10 );
add_action( 'edited_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
add_action( 'created_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
}
/**
* Add term fields form
*/
function add_vendor_custom_fields() {
wp_nonce_field( basename( __FILE__ ), 'vendor_custom_fields_nonce' );
?>
<div class="form-field">
<label for="facebook"><?php _e( 'Facebook', 'domain' ); ?></label>
<input type="url" name="facebook" id="facebook" value="" />
</div>
<div class="form-field">
<label for="twitter"><?php _e( 'Twitter', 'domain' ); ?></label>
<input type="url" name="twitter" id="twitter" value="" />
</div>
<?php
}
/**
* Edit term fields form
*/
function edit_vendor_custom_fields( $term ) {
wp_nonce_field( basename( __FILE__ ), 'vendor_custom_fields_nonce' );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="facebook"><?php _e( 'Facebook', 'domain' ); ?></label></th>
<td>
<input type="url" name="facebook" id="facebook" value="<?php echo esc_url( get_term_meta( $term->term_id, 'facebook', true ) ); ?>" />
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="twitter"><?php _e( 'Twitter', 'domain' ); ?></label></th>
<td>
<input type="url" name="twitter" id="twitter" value="<?php echo esc_url( get_term_meta( $term->term_id, 'twitter', true ) ); ?>" />
</td>
</tr>
<?php
}
/**
* Save term fields
*/
function save_vendor_custom_fields( $term_id ) {
if ( ! wp_verify_nonce( $_POST['vendor_custom_fields_nonce'], basename( __FILE__ ) ) ) {
return;
}
$old_fb = get_term_meta( $term_id, 'facebook', true );
$old_twitter = get_term_meta( $term_id, 'twitter', true );
$new_fb = esc_url( $_POST['facebook'] );
$new_twitter = esc_url( $_POST['twitter'] );
if ( ! empty( $old_fb ) && $new_fb === '' ) {
delete_term_meta( $term_id, 'facebook' );
} else if ( $old_fb !== $new_fb ) {
update_term_meta( $term_id, 'facebook', $new_fb, $old_fb );
}
if ( ! empty( $old_twitter ) && $new_twitter === '' ) {
delete_term_meta( $term_id, 'twitter' );
} else if ( $old_twitter !== $new_twitter ) {
update_term_meta( $term_id, 'twitter', $new_twitter, $old_twitter );
}
}
add_action( 'wcpv_registration_form', 'vendors_reg_custom_fields' );
function vendors_reg_custom_fields() {
?>
<p class="form-row form-row-first">
<label for="wcpv-facebook"><?php esc_html_e( 'Facebook', 'domain' ); ?></label>
<input type="text" class="input-text" name="facebook" id="wcpv-facebook" value="<?php if ( ! empty( $_POST['facebook'] ) ) echo esc_attr( trim( $_POST['facebook'] ) ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="wcpv-twitter"><?php esc_html_e( 'Twitter', 'woocommerce-product-vendors' ); ?></label>
<input type="text" class="input-text" name="twitter" id="wcpv-twitter" value="<?php if ( ! empty( $_POST['twitter'] ) ) echo esc_attr( trim( $_POST['twitter'] ) ); ?>" />
</p>
<?php
}
add_filter( 'wcpv_shortcode_registration_form_validation_errors', 'vendors_reg_custom_fields_validation', 10, 2 );
function vendors_reg_custom_fields_validation( $errors, $form_items ) {
if ( filter_var( $form_items['facebook'], FILTER_VALIDATE_URL ) === false ) {
$errors['facebook'] = __( 'Facebook field format is not correct. Please enter your Facebook page URL.', 'domain' );
}
if ( filter_var( $form_items['twitter'], FILTER_VALIDATE_URL ) === false ) {
$errors['twitter'] = __( 'Twitter field format is not correct. Please enter your Twitter profile URL.', 'domain' );
}
return $errors;
}
add_action( 'wcpv_shortcode_registration_form_process', 'vendors_reg_custom_fields_save', 10, 2 );
function vendors_reg_custom_fields_save( $args, $items ) {
$term = get_term_by( 'name', $items['vendor_name'], WC_PRODUCT_VENDORS_TAXONOMY );
if ( isset( $items['facebook'] ) && ! empty( $items['facebook'] ) ) {
$fb = esc_url( $items['facebook'] );
update_term_meta( $term->term_id, 'facebook', $fb );
}
if ( isset( $items['twitter'] ) && ! empty( $items['twitter'] ) ) {
$twitter = esc_url( $items['twitter'] );
update_term_meta( $term->term_id, 'twitter', $twitter );
}
}
view raw functions.php hosted with ❤ by GitHub

Let’s break it down to understand what it does.

From line 1 to line 50, the code adds the two new fields to the taxonomy. This way, when as an admin you want to manually create or edit a vendor, you can specify their FB and Twitter URLs in the Dashboard.

From line 51 to 76 it validates and saves the fields in the Dashboard, so until now, the code is all to manage the fields in the backend, for administrators.

From line 78 to line 105, it adds the same field to the registration form that you can show on a page by using the shortcode [wcpv_registration]. The fields will appear after the Vendor Description fields, which is a default field shown at the end of the form, before the Submit button.

The last part of the code, from line 107 to the end, saves the fields in the vendor taxonomy.

This is just a simple example with two text fields, as you can imagine you can add any kind of fields, and as many as you want.

One thing that I didn’t mention in the article is how to validate custom fields. I didn’t validate them here because it was not required, but by using the hook wcpv_shortcode_registration_form_validation you can validate the fields before the vendor is created. In example, if you want mandatory fields, you can check if they are empty, and eventually stop the registration and return an error message to the user.

The hook wcpv_shortcode_registration_form_validation accepts two parameters, the first is an array of error messages, the second is an array with all the form fields submitted by the user.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

More Posts That You Might Like…


78 responses to “Adding custom fields to the vendor registration form”

  1. […] Mustone has a tutorial on adding custom registration fields for WooCommerce Product […]

  2. Jameshwart Lopez Avatar
    Jameshwart Lopez

    Can help me about wcpv_shortcode_registration_form_validation it seems like this has a limited documentation and I cant see example code in this hook

    1. Hi,
      The hook allows you to validate existing/custom fields by your own. It accepts 2 parameters, the first is an array of errors, and the second is the array of form fields with their values.

      One example could be this, which checks if the vendor description field is empty or not:

      https://gist.github.com/7a3a4ac33ed70182fb18bece143c03a4

      1. To anyone looking for a more detailed solution, follow the link that gareth’s pasted! Has form validation, captcha etc

  3. Hi there,

    Could I use this to add a captcha field from the Captcha Pro plugin by BestWebSoft? If so, how?

    Thanks,
    Nathan Pinno

    1. Hi Nathan,
      In theory yes, you could, but I’m not able to help you since I never used Captcha Pro.

      Try to check the plugin documentation and see if there’s a shortcode/function to show the field wherever you want.

  4. Hi Nicola – this is v.useful – many thanks. I no wp expert but want to change some of the wording on the Vendor Registration form…for example:

    Rename: ‘Vendor’ to ‘Partner’

    Change ‘Please describe something about your company and what you sell’ to ‘Please describe your work and what you can offer’

    etc

    The input fields are fine – it is simply the text. Id be grateful if you could direct me to the page where I can make these changes.

    Much appreciated
    Ben

    1. Hi Ben,
      You could use the plugin Say What? to replace strings in your website.

  5. I am trying to use this with a dropdown but its never saving my value http://stackoverflow.com/questions/37776498/adding-fields-to-vendor-page-woocomerce any ideas what could be wrong here

    1. Hi,
      The code works, the new value is saved. What is wrong is how you print the current value in the dropdown.

      You use:

      <select name="vendor_size" id="vendor_size" value="<?php if ( ! empty( $_POST['vendor_size'] ) ) echo esc_attr( trim( $_POST['vendor_size'] ) ); ?>"
      

      The select element does not accept the value attribute like that.
      You need to select the option with the respecting value, by using the function selected.

      You will find examples about how to use it in the Codex, follow the link above.

      Thank you!

      1. I still have not got the code to work can you provide code inline with my example that wil get the drop down saving correctly please its so annoying it not working woth showing the correct value from the dropown that is saved it was in the code i posted ealrier

  6. Hi, thank you for this. Can we add custom field in the store settings and make it visible to company’s site?

    Thanks,
    Myko

  7. Hello,
    I had mounted all my website with the old version of product vendors (1.0), i had some different needs but i managed to use this plugin with some modifications. I had created a login area with the “private content” plugin and some other things that i´ve needed, like upload form for large images with “use your drive” plugin directly to my google drive for the vendors, and some more (i dont need the vendor to create their own products and other thing that comes with the plugin). And now with the new woocommerce i had to update to the new version of the product vendors too, because the 1.0 was getting some errors, but in this new version they removed the shortcodes that used to show to my vendors the statistics of comissions!! is there a way to continue using the shortcodes? or some other way to show the comissions to my vendors? I cant use this plugin anymore… thanks!

  8. Thanks for the information. Fields are not saved after submission. I have tested with your code.
    Saw the two custom fields on vendor details but not the value.
    Any idea what went wrong?

    1. I realized it was the problem of the version of the plugin. It works completely fine now.
      Thanks!

    2. Awesome! Thanks for letting me know 🙂

  9. How do I change the title that appears inside the page, from ‘vendor registration form’ to ‘custom text’?

    Thanks!

    1. Hi sam,
      To change that you will need to edit/override the template shortcode-registration-form.php in wp-content/plugins/woocommerce-product-vendors/templates/ since there’s not a filter to do it.

  10. I am trying to get a field in to pull in current Categories into a drop down? So Vendors can pick what category thier products go into, any ideas, suggestions? Working with your code as a basis, ty really helps.

  11. How do I add a text other than links(facebook and twitter) on Vendor Registration form. And how do I add an attachment file?

    1. Hello,
      It would be the same, just instead of using <input type="url" use <input type="text" if it’s a play text field.

      1. Thanks so much for this, it is so useful!! 🙂

        I tried changing the input from “url” to “text” as you suggest but when I save the field the system still adds https:// at the beginning of the field and replaces spaces with %20.

        Any idea on how to fix this?
        Hope you can help me… thanks a lot!

        1. That’s probably because of the esc_url functions used when saving the data from those fields.

          Those are specifically used for URL fields. If it’s just text you can use other functions like sanitize_text_field.

  12. Hi Nicola, how i could add three custom field for allow vendors upload three images? jpg files, thank you

    1. You can just use <input type="file" as field type. The way you save them is different since you need to save the uploaded file as an attachment. You can find how to do that here: https://codex.wordpress.org/Function_Reference/wp_insert_attachment

      1. Is there a way to have the vendor upload their own avatar on registration?

        I’m using your snippet to create a vendor account on registration and would like them to upload their “store image” during registration.

  13. Hi, for some strange reason I no longer get the message “Your request has been submitted, You’ll be contacted shortly” after clicking the register button. Instead I get a horizontal red bar and the form fields are retained with the vendor input after clicking the button. Is there a conflict between formidable pro and this form? I recently updated the formidable pro and this was experienced after it.

    1. Hello, I’m sorry but I never heard of formidable pro before. I’d suggest you to disable it and see if it works, in which case it indicates a compatibility issue that their developers should be able to fix.

  14. What if I want to save the vendor name as the company name in the billing information of the user profile ?
    I tried adding different hooks that did not work :
    – user_register
    – woocommerce_save_account_details
    Do you have any idea ?

  15. Any idea on how to add the fields in the store settings of PV ?

  16. ifeanyi ejindu Avatar
    ifeanyi ejindu

    Thanks so much for your post.
    I would like to know how to be able to charge vendors on time fee if they want to sell certain category of products. and a way to add to or reduce the list of category registrants can pay for in the future.

    1. Hi,
      I’m sorry but I don’t think this is easily doable or explainable in a blog comment. I think you should search for a professional developer at:

  17. Hi Nicola, your explanation has helped me to create a form as my client wanted it. Now I would like to create a second form, same as the previous one but I do not know how to duplicate it.

    Thank you very much and greetings.

    1. Where do you want to create this second form?

  18. Sal Baldovinos Avatar
    Sal Baldovinos

    Thanks for the tut! What would be the best way to simply EDIT (vs adding) exisitng information? ie – change “Vendor Name” to something else and the Form Title?

    1. Hi Sal,
      to edit the existing fields you can override the template from /wp-content/plugins/woocommerce-product-vendors/templates/shortcode-registration-form.php to /wp-content/themes/your-child-theme-name/woocommerce-product-vendors/shortcode-registration-form.php.

  19. Hello,
    I had mounted all my website with the old version of product vendors (1.0), i had some different needs but i managed to use this plugin with some modifications. I had created a login area with the “private content” plugin and some other things that i´ve needed, like upload form for large images with “use your drive” plugin directly to my google drive for the vendors, and some more (i dont need the vendor to create their own products and other thing that comes with the plugin). And now with the new woocommerce i had to update to the new version of the product vendors too, because the 1.0 was getting some errors, but in this new version they removed the shortcodes that used to show to my vendors the statistics of comissions!! is there a way to continue using the shortcodes? or some other way to show the comissions to my vendors? I cant use this plugin anymore… thanks!

  20. Hi Nicola, I really appreciate this code example.
    Do you know how I might get the fields to save on the Vendor Store Settings view? I can edit the individual Vendor’s settings as an Administrator but the Vendors can’t update the custom fields. (html-vendor-store-settings-page.php) but I can’t get them to populate or save from on that form.

  21. Hi, thanks for tutorial, very useful.
    I noticed that “The fields will appear after the Vendor Description fields, which is a default field shown at the end of the form, before the Submit button.”.
    I want my fields to appear before vendor description, have any solution for reordering that.?
    Thank you

  22. Hi Nicole. Thanks for the tutorial. I was able to use the code above to enable vendors add their account details. The information from the form is visible for the admin to update. However, I want this information to update on the vendors profile page in the vendors dashboard. So, if the vendors have any change, they can update their details. Also, when they update their details, I want the admin to able to see the updated information. How can I achieve this? Thanks Nicole.

  23. This is a really useful guide. Thanks for your perpetually great work.
    I have been able to add custom fields to some of my sites.
    I am hoping I can add captcha to the vendor registration form. How can I go about it?

    Thanks Nicola.

    1. Hey there,
      Basically it’s the same thing but it also requires javascript to make the captcha work.

      That would be like an entire new article just for this 🙂

  24. Hi Nicola,

    How about adding billing address and billing phone on registration form using the WCPV registration shortcode? I understand that the fields are already on the Users profile in the WP admin, but I am having a hard time on saving these billing data.

    Thanks.

  25. Very useful and helpful article – thank you! Although I’m unsure how we can then extract these custom fields for output/display?

    1. Hi Nick,
      The values are term meta, so you can use get_term_meta to get them.

  26. Hi,
    How can I show te fields on the “wcpv-vendor-settings”-page. So vendors can edit the custom fields whenever they want.

  27. Thanks for this code, very helpful! How do I make these custom fields required?

    1. Hi Chanel,
      You can try to add the required attribute to the input fields.

  28. I noticed that “The fields will appear after the Vendor Description fields, which is a default field shown at the end of the form, before the Submit button.”.
    I want my fields to appear before vendor description, have any solution for reordering that.?
    Thank you

    1. Not really. The form has 3 actions, one at the beginning of the form, one before the submit button and one after it.

  29. Hi, Shall i create a password there itself

    1. Sorry, I don’t understand. Can you explain?

  30. I fail to see why so many people have made themselves dependent on the whole woo atmosphere as it seems to be the clumsiest jumble of coding in the WordPress environment. Maybe It should develop a fork for woo folks who keep coding in the age of DOS. Why on earth an end user in 2018 should be wading through reams of code to add two form fields, and would have to do it all over if they change a theme with one mouse click, is beyond me.

    That said through my halo though, thanks for the snippet because having only the name and email address for someone you trust to deliver goods or services to your site’s users, is a hell of a risk. Amazon requires a piece of government ID on top of all your bio data. :-;

    1. Hi Mark,

      I fail to see why so many people have made themselves dependent on the whole woo atmosphere as it seems to be the clumsiest jumble of coding in the WordPress environment.

      then why do you use WooCommerce? It seems that you are not happy with it, but still use it.

      You’re welcome for the snippet!

  31. Hi Nicola

    I’m building a website using vendor and also I add a WPML Plugin (WordPress Multilingual) for Japanese and Spanish. But doesn’t make any effect on the registration form and of course I need to do it manually.

    I don’t usually code and I need a tip to start and don’t ruin it. Please help on this one! Thank you

    1. Hi Gerardo,
      I’m sorry but I’m not sure what the issue is here.

    2. I want to give the option for my users in Spanish, Japanese and English in the registration form.

  32. Hi Nicola! Great post!

    I have followed your tips and works great! I have added a new field without any trouble.
    However, there is a way to add this custom fields to the notification email sent to the administrators? The email template is vendor-registration-email-to-admin.php, but I think that I don’t have access to the new custom fields created… or at least how to access to them…

    Thanks!

  33. I just want to change the fieldnames from the form. Is this also possile? Because I want to change it in German…
    Hopefully you can help me, I found nothing about it, only how to add the boxes for facebook and twitter.

  34. How to show these fields on thank you page???

  35. $vendor_vpa = get_term_meta( $term->term_id, ‘upivpa’, true );
    echo #vednor_vpa

    above code is not working on thank you page. Its not getting term id/vendor id on thank you page using: $term->term_id OR $term_id OR $vendor_id.

    None of the above is working on Woo thank you page

    but, if i am trying:
    $vendor_vpa = get_term_meta( 42, ‘upivpa’, true );
    echo #vednor_vpa

    its working.

    Please help. How to get vendor id or term id of product vendor on woo thank you page
    ???????

  36. Hi! Is there an option to get the custom fields from shortcode-registration-form? I mean, if you want the vendor fill the form and the data goes to the same place that the your mode (by functions.php) goes?

    I hope you have an answer, I really need some help!

    Regards!

  37. Hi! Someone here know how to make existing fields not required? By reading comments and documentation I learned how to edit (add or delete) fields but not how to make them not required field.

  38. How do we get the custom fields to appear in the vendor dashboard/store settings area? I noticed a few others with the same question but did not see a response. Any ideas? Thanks!

  39. hi Nicola,

    this adds the inputs to the form, but how to add them to the Store Settings page, so vendors can edit them after signup?

    here: /wp-admin/admin.php?page=wcpv-vendor-settings

    Thanks!

  40. Hi There,

    Is this code still working on WooCommerce Product Vendors – I have tried it multiple times and I am having no luck.

    The code crashed my site and says Critical Error.

    Are you in a position to assist me by any chance?

    Regards
    Rob

  41. Similarly How to add upload file field and multi select checkbox fields.

  42. The form validation doesn’t appear to work properly. The validation itself works but when all fields are filled in, i just get a blank red error box and the form will not submit. I am using wp_send_json( array( ‘errors’ => $errors ) )
    Any ideas?

    1. Hello,
      I updated the post with code to include validation of the form with the shortcode.

  43. Hi i try to Add an image Field On The Vendor Registration Form In Dokan.
    does anyone have solution please.

    1. Hi, I am also looking for a solution for this. But I have not been able to find any. Were you able to get the custom file upload field working?

  44. Hi,
    I would like to know what if already register member want to join as vendor?
    Email validation and Username validation blocking existing user to become a vendor.
    Any solution of this issue?

  45. I am trying to add a set of radio buttons to choose from in the registration form. I can get them to show up, but doesn’t look like the selected value is being saved. Any help or insight is greatly appreciated!

  46. Nicola – thanks for this great code. I successfully added the fields to my form. My fields – 3 text fields do not need validating so I commented out the following:

    /*add_filter( ‘wcpv_shortcode_registration_form_validation_errors’, ‘vendors_reg_custom_fields_validation’, 10, 2 );
    function vendors_reg_custom_fields_validation( $errors, $form_items ) {
    if ( filter_var( $form_items[‘referencename’], FILTER_VALIDATE_URL ) === false ) {
    $errors[‘referencename’] = __( ‘Reference Name field format is not correct. Please enter your Reference Name.’, ‘text’ );
    }

    if ( filter_var( $form_items[‘referencetelephone’], FILTER_VALIDATE_URL ) === false ) {
    $errors[‘referencetelephone’] = __( ‘Reference Telephone field format is not correct. Please enter your Reference Telephone.’, ‘text’ );
    }
    if ( filter_var( $form_items[referencerelationship], FILTER_VALIDATE_URL ) === false ) {
    $errors[referencerelationship] = __( ‘Reference Relationship field format is not correct. Please enter your Reference Relationship.’, ‘text’ );
    }

    return $errors;

    }
    */

    Unfortunately, when I click submit, my screen freezes (hour glass) and I don’t see “Your request was submitted.” Did I comment out something I need?

    Also, btw, the vendor data does get added on the backend.

    Thank you,
    Lynne

  47. I do not have the issue above anymore. I have resolved it. I do wish you would share how to add the new field(s) to the emails sent to admins and/or vendors. Thanks.

  48. Hello my colleague,
    I want to add this field.
    Field is required
    XX YYYYYYYY (VAT number)
    X = letter A-Z or EU countries code
    Y = digit 0-9
    Please enter a valid EU tax number. If otherwise, registration will be canceled.
    Please help me.

  49. Happy new year
    Is there anyone to help me
    In register form how we add new radio button with different role. Also i want new form fields.

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: