Saltar navegación

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

This support ticket is created 4 years, 1 month 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 -

Zona horaria del colaborador: Asia/Kolkata (GMT+05:30)

Este tema contiene 6 respuestas, tiene 2 mensajes.

Última actualización por catherineB 4 years, 1 month ago.

Asistido por: Minesh.

Autor
Mensajes
#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 (enlace oculto...) in the 'whatever-value-you-like-or-url-to-image' part or the code. It is ok?

Form page: enlace oculto

#2356067

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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', 'enlace oculto');
}
}
}

Image slug: photo
Image link: enlace oculto
CRED form ID: 20129

#2356349

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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>enlace oculto</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: enlace oculto (no image on the top left section)

#2356377

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Could you please check now: enlace oculto

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

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>enlace oculto</u></em>');

         }
     }
}

I can see the default image is set:
=> enlace oculto

#2356383

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