Check-out our new look and give us some feedback!

How To Add Custom Registration Fields Using Product Vendors?

Posted on by AJ Morris
Reading Time: 3 minutes

As the internet evolves, so does the information you need from customers when they register with your store. It’s likely that a default registration form will cover everything you want to gather when some signs up. For example, having social media usernames can help you keep in touch, but those fields are not automatically part of the default form.

It’s also important that you be able to add the same information if you manually create a user within the dashboard itself. Adding and modifying a bit of code in your theme’s function.php file will allow you to do both.

/**
 * 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_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 );
	}
}

Let's break down this code a bit.

Lines 1 to 50, adds the two new fields to the taxonomy in the Product Vendors form. This allows you to specify their Facebook or Twitter URLs as to manually create or edit a vendor in the WP-Admin.

Lines 51 to 76, validates and saves the fields in the database.

Lines 78 to 91, add the same fields to the registration form. You can show this on a page by using the shortcode:

[wcpv_registration]

The fields will appear after the Vendor Description fields. These are shown at the end of the form right before the Submit button.

Lastly, lines 93 to 98 saved the fields in the vendor taxonomy.

Of course, this is a simple example with two text fields. You could easily add any kind of field you want, even more than 2.

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these techniques today!

About the Author: AJ Morris

AJ Morris is the Product Innovation and Marketing Manager at iThemes. He’s been involved in the WordPress community for over a decade focusing on building, designing and launching WordPress websites and businesses.

Latest Articles

How to Edit Your DNS Hosts File

Read Article

How to Edit Your DNS Hosts File

Read Article

Microsoft Exchange Server Security Update

Read Article

How to Monitor Your Server in WHM

Read Article

How to Monitor Your Server in WHM

Read Article