Skip Navigation

[Resolved] Cred if no product featured image selected set one automatically

This support ticket is created 4 years 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 9 replies, has 2 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1591241

I have a post product form where i have added a field for uploading the featured image . i have tried to set an image if no image was selected, i am not sure what i a missing.

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']==222)
    {
        $fimg = get_post_meta( $post_id, '_featured_image');
        if(isset($fimg)) {
          $img = $fimg;
          $attachment_id = attachment_url_to_postid( $img );
          update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
        }
      	if(!isset($fimg)) {
            $url = '<em><u>hidden link</u></em>';
    		$title = "Fifa 2020";
    		$image = media_sideload_image( $url, $post_id, $title, 'id' );
    		set_post_thumbnail( $post_id, $image );
		}
        //}
    }
}

If i use the following function, it sets a featured image automatically, just as expected.

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']==222)
    {
            $url = '<em><u>hidden link</u></em>';
    		$title = "Fifa 2020";
    		$image = media_sideload_image( $url, $post_id, $title, 'id' );
    		set_post_thumbnail( $post_id, $image );
    }
}

the only thing is missing that it's not saving the featured image if it was chosen

#1591865

Okay if the featured image is not set in the Form, this get_post_meta call will return an empty array because you did not pass the 3rd parameter (default value is false):

$fimg = get_post_meta( $post_id, '_featured_image');

So without a featured image, $fimg will be set, but it will be set as an empty array. If you pass true as the 3rd parameter, then you can check if not $fimg:

$fimg = get_post_meta( $post_id, '_featured_image', true);
        if(!($fimg)) {
          // set the default featured image here
        }

If the featured image is set in the Form, I don't think you need to do anything special - Forms will set the featured image using this information automatically. In other words, this should be deleted:

if(isset($fimg)) {
          $img = $fimg;
          $attachment_id = attachment_url_to_postid( $img );
          update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
        }

Another idea, instead of using cred_save_data after the fact, is to use the cred_before_save_data API hook to check and set the featured image URL before the post is ever created. https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        if ($_POST['_featured_image'] == '')
        {
           $_POST['_featured_image'] = '<em><u>hidden link</u></em>';
        }
    }
}
#1592695

I think your latest solution is great and should work.

BUT the issue now is different. It looks like i can't save featured image if "Use the WordPress Media Library manager for image, video, audio, or file fields " is checked in the toolset form.

1- I have disabled all plugins just kept Toolsets needed plugins and Woocommerce
2- Switch to Twenty Twenty theme
3- Disabled all custom snippets and the above code
4- Created a completely new form for adding product with Toolset wizard

nothing form what i did helped to save the featured image. only if i uncheck "Use the WordPress Media Library manager for image, video, audio, or file fields " then it saves the featured image as expected!

#1592711

BTW.

I have tested follwoing code of your solution but seems it's not working as well even if the field "Use the WordPress Media Library manager for image, video, audio, or file fields" unchecked

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==222)
    {
        if ($_POST['_featured_image'] == '')
        {
           $_POST['_featured_image'] = '<em><u>hidden link</u></em>';
        }
    }
}
#1593149

Okay yes, I see that I did not check my code thoroughly and the solution is not so simple. Further testing shows that cred_before_save_data hook will not be effective here, unfortunately.

Also, I was testing with the Media Library active and I can confirm the problem you experienced - featured images are not saved! This appears to be a bug, and I'm asking my 2nd tier support team to take a closer look. So far I have not been able to get the featured image to save correctly when the WordPress Media Library is active. I'll let you know if I receive a workaround.

So would you like to go back to the cred_save_data hook with Media Library turned off?

#1593347

Yes, for now i got back to the upload image button instead the Media Library using following code and it works. but would be great if we solve the issue with Media Library

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']==222)
    {
      	if ($_POST['_featured_image'] == '')
        {
            $url = '<em><u>hidden link</u></em>';
            $title = "Image Name";
            $image = media_sideload_image( $url, $post_id, $title, 'id' );
            set_post_thumbnail( $post_id, $image );
         }
    }
}
#1593375

Okay yes I have escalated the Media Library issue to my 2nd tier support team and I will let you know what I find out.

#1597513

FYI this issue has been escalated to our developers and an erratum post has been published here: https://toolset.com/errata/product-image-is-not-added-to-new-products-created-with-toolset-forms/

Our developers will address the problem in an upcoming release of the software. Feel free to subscribe to comments in the erratum post for the latest information about this issue and any workarounds or patches that may become available.

#1627941

Hello, the problem with uploading featured images to products in a Form should be resolved in the upcoming Forms 2.5.7 release. I will keep you posted here as I receive more information about the release date.

#1653313

Hello, just a follow up to let you know that Forms 2.5.7 has been released, and the issue where featured images are not saved when the Media Library is active should be resolved. Please feel free to update to the latest versions of all Toolset plugins and let me know if featured images are still not saved as expected when the Media Library is active for this Form.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.