Skip Navigation

[Resolved] Auto-generating field content for empty fields in optional front-end post forms

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED form that includes an optional image field. If the User submits the form without including an image in the field, I would like to automatically choose 1 of 4 placeholder default images at random.

Solution:
Add this custom code to your child theme's functions.php file:

add_action('cred_save_data', 'cred_add_default_img_action',10,2);
function cred_add_default_img_action($post_id, $form_data)
{
    $forms = array( 1234, 5678 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (!isset($_POST['wpcf-slug']))
        {
            $pics = array(
              "http://yoursite.com/path/to/image1.jpg",
              "http://yoursite.com/path/to/image2.jpg",
              "http://yoursite.com/path/to/image3.jpg",
              "http://yoursite.com/path/to/image4.jpg",
            );
     
            add_post_meta( $post_id, 'wpcf-slug', $pics[ rand( 0,3 ) ] );
        }
    }
}

Replace 1234, 5678 with a comma-separated list of CRED form IDs where you want to apply this code. Replace 'wpcf-slug' with your field slug, and add the 'wpcf-' prefix, replace the 4 URLs with the actual image URLs on your site. If you choose not to use 4 options, replace the '3' in 0,3 with a number one less than the number of default options.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
http://php.net/manual/en/function.rand.php
http://php.net/manual/en/language.types.array.php

This support ticket is created 6 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by JanP6468 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#622651

In case user leaves a voluntary field ("bike photo") empty in a front-end post form (where she creates a profile of her bicycle), is there a possibility to "auto-generate" the image (pick one of the pre-selected images)? I did not find such option in the documentation.

What is the link to your site?
fietsid.com

Thank you

#622769

Hi, I think there are two approaches here.
1. Do not automatically assign any default value, and instead use conditional HTML on the front-end to display the default value whenever the custom field is empty. More information about conditional HTML here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

2. Automatically assign a default image using custom code. If the User edits their post in the future, the default image will be pre-set and they must remove it to add a different picture. Here's a code snippet you can add to your child theme's functions.php file:

add_action('cred_save_data', 'cred_add_default_img_action',10,2);
function cred_add_default_img_action($post_id, $form_data)
{
    $forms = array( 1234, 5678 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (!isset($_POST['wpcf-slug']))
        {
            add_post_meta($post_id, 'wpcf-slug', '<em><u>hidden link</u></em>');
        }
    }
}

Replace 1234, 5678 with a comma separated list of form IDs where you want to apply this default image override, then replace 'wpcf-slug' with the slug of your custom field, prefixed with 'wpcf-'. For example if your custom field slug is bike-image then you should use 'wpcf-bike-image' here. Finally replace hidden link with the actual URL of the default image.

#622897

Christian, you are amazing. Truly my favourite pro here at this forum. I took the second option and it works like a charm.

I have one more question though: is there a (relatively simple) way to variate 3 or 4 pictures as defaults?

Explanation: we are building a user-generated database where the entries are open and visible, and many users do not fill all fields (photos are one of the least favourite fields, although they visible at first glance on the database). We are replacing the bike photo with a dummy icon, and it would be awesome if we could variate couple colours of the icons, so the database doesn't look like a load of monochrome icons. Of course, we have the icon in multiple colours, so we need the system to randomly (or systematically, doesn't really matter) pick one of those pictures.

If there is no way, we can live without it - it is "only" user's aesthetical experience we are trying to fix here.

Thank you so much for your help!

Jan

#623012

You can put all the possible image URLs in an array, then use PHP rand() to select one of those images:

add_action('cred_save_data', 'cred_add_default_img_action',10,2);
function cred_add_default_img_action($post_id, $form_data)
{
    $forms = array( 1234, 5678 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (!isset($_POST['wpcf-slug']))
        {
            $pics = array(
              "<em><u>hidden link</u></em>",
              "<em><u>hidden link</u></em>",
              "<em><u>hidden link</u></em>",
              "<em><u>hidden link</u></em>",
            );
    
            add_post_meta( $post_id, 'wpcf-slug', $pics[ rand( 0,3 ) ] );
        }
    }
}

The comma separated list of image URLs can be any length. If you add or remove options in the comma-separated list, you must replace the "3" in rand( 0,3 ) with a number that is the total number of options minus 1. For example if your list has 10 items, this number should be 9.

#623253

Yet again, worked like a charm. Thanks so much Christian!