Navigation überspringen

[Gelöst] Default image if user do not add one in CRED FORM

This support ticket is created vor 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 -

Zeitzone des Unterstützers: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von catherineB vor 4 years, 1 month.

Assistiert von: Minesh.

Author
Artikel
#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 (versteckter Link...) in the 'whatever-value-you-like-or-url-to-image' part or the code. It is ok?

Form page: versteckter Link

#2356067

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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', 'versteckter Link');
}
}
}

Image slug: photo
Image link: versteckter Link
CRED form ID: 20129

#2356349

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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>versteckter Link</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: versteckter Link (no image on the top left section)

#2356377

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Could you please check now: versteckter Link

I've moved the custom code to "Custom code" section offered by Toolset:
=> versteckter Link

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>versteckter Link</u></em>');

         }
     }
}

I can see the default image is set:
=> versteckter Link

#2356383

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