Skip Navigation

[Resolved] Cred form – Upload default feature image and save it with post if none uploaded

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.

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by GeorgeM4735 6 years, 2 months ago.

Assigned support staff: Shane.

Author
Posts
#320234

This is sort of a continuation of my previous resolved post here:
https://toolset.com/forums/topic/cred-form-upload-default-feature-image-if-none-uploaded/

Even though I though the issue was resolved, there are still issues. To reiterate:

I have an optional feature image upload button on my form that creates a normal post. I was wondering if it was possible to attach a default image if no image has been uploaded. Setting if statements inside the view will not be enough. I actually want the default image to be SAVED as a feature image with the post instead of displaying it in a view. And this needs to happen only when a user does not include a feature image before he/she hits submit. I need to have the default image actually saved as a feature image because I am presenting the posts through a different plugin and I need the feature iamge to be part of the post uploaded as a feature image, if you know what I mean.

I tried using cred_before_save_data hook:

//Save default feature image
add_action('cred_before_save_data', 'save_default_feature_image_action',10,1);;
function save_default_feature_image_action($form_data)
{
     // if a specific form
    if ($form_data['id']==4248)
    {
        if (empty($_POST['_featured_image'])) {
            $_POST['_featured_image'] == "path-to-image/image.png"; 
        }   
    }
}

but it doesn't work.

#320291

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi George,

Please try using this code below.

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']==12)
    {
        if (empty($_POST['_featured_image']))
        {
            // add it to saved post meta
     add_post_meta( $post_id, "_thumbnail_id",'22' );        
        }
    }
}

What you need to do is replace the 22 with the image id. You can know the image ID by going to Media Gallery and editing the image.

The media ID will appear in the url example.
hidden link

You will see post = 38 , this will be the attachment id .

Please let me know if this helps.

Thanks

#320305

That worked like a charm! Thanks again Shane!