1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Make a field required in the dashboard

Make a field required in the dashboard

There will be times when you want to make a field required that is not available in our form options you can use the following steps. This utilizes our Form helper and validation system.

  • Find the form field filter you need
  • Create a new snippet

Example make product dimensions required

The following snippet will be located in your themes functions.php. Ensure that you are adding this to a child theme and not a parent theme. This will make your changes upgrade safe.

The field filters are the following

  • wcv_product_length
  • wcv_product_width
  • wcv_product_height

These filters are found in the WCVendors_Pro_Product_Form class this file is located in public/forms/class-wcvendors-pro-product-form.php

The filter override for this will be as follows

add_filter( 'wcv_product_length', 'make_field_required' ); 
add_filter( 'wcv_product_width', 'make_field_required' ); 
add_filter( 'wcv_product_height', 'make_field_required' ); 
function make_field_required( $field ){ 
    $field['custom_attributes'] = array( 'required' => '' );
    return $field; 
}

This will add the required attributes to make the 3 product dimension fields required as seen in the screenshot below.

I want to make another field required

If you want to make a different field required, all you would need to update is the add_filter part of the snippet.

For example, include product weight as required as well as the dimesions.

The product weight filter is

  • wcv_product_weight
// Make the product dimensions required 
add_filter( 'wcv_product_length', 'make_field_required' ); 
add_filter( 'wcv_product_width', 'make_field_required' ); 
add_filter( 'wcv_product_height', 'make_field_required' ); 
// Make the product weight required 
add_filter( 'wcv_product_weight', 'make_field_required' ); 
function make_field_required( $field ){ 
    $field['custom_attributes'] = array( 'required' => '' );
    return $field; 
}

filters

Updated on February 1, 2021

Was this article helpful?