Skip Navigation

[Resolved] Default image if user do not add one in CRED FORM

This support ticket is created 2 years, 6 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
- 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 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by catherineB 2 years, 6 months ago.

Assisted by: Minesh.

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

Form page: hidden link

#2356067

Minesh
Supporter

Languages: English (English )

Timezone: 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', 'hidden link');
}
}
}

Image slug: photo
Image link: hidden link
CRED form ID: 20129

#2356349

Minesh
Supporter

Languages: English (English )

Timezone: 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>hidden 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: hidden link (no image on the top left section)

#2356377

Minesh
Supporter

Languages: English (English )

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

Could you please check now: hidden link

I've moved the custom code to "Custom code" section offered by Toolset:
=> hidden 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>hidden link</u></em>');

         }
     }
}

I can see the default image is set:
=> hidden link

#2356383

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