Skip Navigation

[Resolved] Required field settings for post parent and featured image

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to make the post parent field and featured image field required in my CRED form.

Solution: The parent post CRED field shortcode will accept a required='true' attribute like this:

[cred_field field='_wpcf_belongs_slug_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap' required='true']

The featured image shortcode will not accept a required='true' attribute, so the only way to require a featured image is to use custom code. The CRED API cred_form_validate can be used to make the featured image required. Add this code to your child theme's functions.php file:

add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
  $forms = array( 1234, 5678 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if (empty($fields['_featured_image']['value'])) {
      $errors['_featured_image'] = __( 'Featured image is required', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

Replace 1234, 5678 with a comma-separated list of any CRED form IDs where you want to make the featured image required.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#620211

How to set WordPress fields (ie 'Featured Image') and Relationship fields (ie parent post type Slection field of 'Belongs to Country' - 'Country' drop down field is pulled by parent post type) as obligatory fields (in CRED)?

#620290

The parent post CRED field shortcode will accept a required='true' attribute like this:

[cred_field field='_wpcf_belongs_slug_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap' required='true']

The featured image shortcode will not accept a required='true' attribute, so the only way to require a featured image is to use custom code. The CRED API cred_form_validate can be used to make the featured image required. Add this code to your child theme's functions.php file:

add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
  $forms = array( 1234, 5678 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if (empty($fields['_featured_image']['value'])) {
      $errors['_featured_image'] = __( 'Featured image is required', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

Replace 1234, 5678 with a comma-separated list of any CRED form IDs where you want to make the featured image required.
More information about this API: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#620328

Thank you!