Skip Navigation

[Résolu] Featured image as required field

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
I would like to have featured image as a required field in CRED form.

Solution:
Add following code in theme’s functions.php file:

function my_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
   
    if ($form_data['id']==488) //change form ID
    {
        if (empty($fields['_featured_image']['value']))
        {
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
   
    return array($fields,$errors);
}
add_filter('cred_form_validate','my_validation',10,2);

Also check that there should be no php errors and syntax mistakes in theme’s OR Child theme’s functions.php

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

This support ticket is created Il y a 7 années. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par toolset-dave Il y a 7 années.

Assisté par: Noman.

Auteur
Publications
#586893

Hi,

I would like to have featured image as a required field, so I tried this code:

function my_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
  
    if ($form_data['id']==488)
    {
        if (empty($fields['_featured_image']['value']))
        {
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
  
    return array($fields,$errors);
}
add_filter('cred_form_validate','my_validation',10,2);

But there must be something wrong, because the field is still not required.

#587005

Noman
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Karachi (GMT+05:00)

Hi David,

Thank you for contacting Toolset support. I have checked the code you have provided, and it is working as expected.

- Please double check the form ID.
- Please try to check it by deactivating all third-party plugins (except Toolset) and switching back to the Default Theme (e.g. Twenty Sixteen theme) to see for any possible conflicts with any of the plugins or themes?

- If still issue arise, please provide temporary access (WP-Admin and FTP Login info) to your staging site.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Links to the [CRED Form] Edit Screen and [Page] Edit Screen where you have inserted this Form.

Thank you

#587122

Noman
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Karachi (GMT+05:00)

Hello David,

There were some php errors and syntax mistakes in Child theme’s functions.php. I have fixed the below code and now featured image is made as required on both forms.

function sort_and_format_post_date_fields_func($atts)
{
  $atts = shortcode_atts( array(
    'postid' => '',
    'field' => '',
    'format' => 'j. F Y \o\d G:i'
	), $atts);
  $values = get_post_meta($atts['postid'], 'wpcf-datum-konani');
  usort($values, 'numbersort');
  $string = '';
  foreach($values as $value) {
    $date = date_i18n($atts['format'], $value);
    $prefix = $value < strtotime('0:00:00') ? '<span style="text-decoration:line-through;">' : '';
    $suffix = $value < strtotime('0:00:00') ? '</span>' : '';
    $string .= ($string == '' ? '' : '<br>') . $prefix . $date . $suffix;
  }
 
  return $string;
}
$form_ids[] = 292;
function validate_featured_image_size( $field_data, $form_data ){
 
    $form_id = array( 292,488 ); // add IDs of CRED forms
 
    $target_width = 300; // Edit
    $target_height = 200; // Edit
 
    if ( in_array( $form_data['id'], $form_id ) ) {
 
        // Split field data into field values and errors
        list( $fields,$errors ) = $field_data;
 
 		if (empty($fields['_featured_image']['value']))
        {
            $errors['_featured_image'] = 'Missing featured image';
        }
		else {
			$check = getimagesize( $fields['_featured_image']['value'] );
			if ( $check !== false ) {
 
				$width = $check[0];
				$height = $check[1];
 
				if ( $width !== $target_width || $height !== $target_height ) {
					$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
				}
        	}
		}
		
        $field_data = array($fields,$errors);
    }
 
    return $field_data;
}

Here is your older functions.php file (backup):
lien caché

Thank you

#587314

Hi Noman,

thank you for the new code, it works great and also thanks for fixing my functions.php.

You can delete the backup.

#587403

Noman
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Karachi (GMT+05:00)

Sure. Please mark this ticket resolved as all is good now.

Have a great day,
Thank you

#587461

OK