Passer la navigation

[Résolu] Default image if user do not add one in CRED FORM

This support ticket is created Il y a 4 years, 1 month. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

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

Dernière mise à jour par catherineB Il y a 4 years, 1 month.

Assisté par: Minesh.

Auteur
Publications
#2355779

I want to add a default value for an image field in case a user do not add one while filling up the CRED form.
I used the code from the tread https://toolset.com/forums/topic/using-default-image-if-user-submits-form-without-image/

I added the code in my theme's function.php file and validated fields names / form ID, but it doesn't work
I added the full url (lien caché...) in the 'whatever-value-you-like-or-url-to-image' part or the code. It is ok?

Form page: lien caché

#2356067

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please share the code you are using and let me check and review that the code you are using is correct or not.

Please share the custom field image slug and the default image URL you want to set.

#2356297

//Automatically adding a default image if none is uploaded
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==20129)
{
if (!isset($_POST['photo']))//Change "my_custom_field" to your wpcf-field
{
// since $_POST['photo'] is not set (!isset())
update_post_meta($post_id, 'photo', 'lien caché');
}
}
}

Image slug: photo
Image link: lien caché
CRED form ID: 20129

#2356349

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please try to use the following code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ($form_data['id']==20129) {
      
        if (!empty($_POST['wpcf-photo']) ) {
                          // since $_POST['photo'] is not set (!isset())
                        update_post_meta($post_id, 'wpcf-photo', '<em><u>lien caché</u></em>');
}
}
}

The thing is that Types custom field slug is prefix with 'wpvf-'{custom-field-slug}, so if you have custom field photo, internally it will be stored as wpcf-photo.

Could you please try above code and check if that help you to resolve your issue.

#2356357

Hi Minesh,
I copy-pasted the code, but it doesn't work 🙁
See the result: lien caché (no image on the top left section)

#2356377

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please check now: lien caché

I've moved the custom code to "Custom code" section offered by Toolset:
=> lien caché

add_action('cred_save_data', 'func_set_default_image_cred_form',10,2);
function func_set_default_image_cred_form($post_id, $form_data) {
// if a specific form
if ($form_data['id']==20129) {
        
        if (isset($_POST['wpcf-photo']) and empty($_POST['wpcf-photo']) ) {
                  update_post_meta($post_id, 'wpcf-photo', '<em><u>lien caché</u></em>');

         }
     }
}

I can see the default image is set:
=> lien caché

#2356383

Good to know about that "Custom code" section 🙂
It works perfectly
Thanks Minesh 🙂