Skip Navigation

[Resolved] Featured image (Media Library manager) as required field

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

Problem:

Validate featured image in Toolset post form.

Solution:

You can try the cred_form_validate filter hook, like this:

Put below PHP codes into your theme file "functions.php":

add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
	$forms = array( 225 );
	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;
}

Relevant Documentation:

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

This support ticket is created 3 years, 7 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by gregM-8 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2082163

There is a solution for making regular featured image upload field required ( https://toolset.com/forums/topic/featured-image-as-required-field/ ), but it doesn't work if "Use the WordPress Media Library manager for image, video, audio, or file fields " form setting is checked.

Form settings: hidden link
Form screenshot: hidden link

Just to confirm, here is the code I am using:
/******************************************************************************/
add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
$forms = array( 9681,9682,9673,9683,9679,9677,9675,9672,9680,9678 );
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;
}

#2082373
featured-image-validation.JPG

Hello,

I have tried the PHP codes you mentioned above in the theme file "functions.php", it works fine. See my screenshot featured-image-validation.JPG

If you are using Toolset Snippet to setup those custom codes, please try to move those custom PHP codes into your theme file "functions.php", and test again, since some filter/actions API hooks are triggered before Toolset Snippet is loaded.

#2083293

I have just realized that the Featured Image is being validated only AFTER all other fields are validated and there is no other errors. That's the trick... The issue is now resolved!

#2083295

My issue is resolved now. Thank you!