Issues Setting Featured Images with Formidable Forms File Uploads in Custom WordPress Post Types

By: leena aikawa | Asked: 08/23/2024
ForumsCategory: Code HelpIssues Setting Featured Images with Formidable Forms File Uploads in Custom WordPress Post Types
leena aikawaleena aikawa asked 3 weeks ago

Hello Formidable Forms Community, I’m currently experiencing an issue when trying to set a featured image in custom WordPress post types using Formidable Forms. I have two forms set up, and each is designed to handle image uploads that should be used as the featured image for a corresponding post.  

Setup Details:
  • Formidable Forms Plugin Version: [Plugin Version Here]
  • WordPress Version: [WordPress Version Here]
  • Form IDs and Field IDs:
    • Form ID: [ID X], Image Field ID: [Field ID Y]
    • Form ID: [ID A], Image Field ID: [Field ID B]
  • Custom Post Types:
    • custom-post-type-1 (for Form ID X)
    • custom-post-type-2 (for Form ID A)

 

What I'm Trying to Achieve:

I’m trying to upload an image via Formidable Forms and then use that image as the featured image (post thumbnail) of the corresponding custom post type (custom-post-type-1 or custom-post-type-2). The image upload works, but the process of setting the image as the featured image (post thumbnail) doesn’t seem to be functioning correctly.  

The Code I’m Using:

Here’s the PHP code I’m using to handle the image uploads and set the featured image:

 

if (!function_exists('media_handle_sideload')) {
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
} function handle_formidable_img_upload($entry_id, $form_id) {
if ($form_id == [ID X] || $form_id == [ID A]) {
$entry = FrmEntry::getOne($entry_id, true);
$post_id = $entry->post_id; $field_id = ($form_id == [ID X]) ? [Field ID Y] : [Field ID B]; if (!empty($entry->metas[$field_id])) {
$attachment_id = absint($entry->metas[$field_id]); if ($attachment_id) {
set_post_thumbnail($post_id, $attachment_id);
error_log('Featured image set: ' . $attachment_id);
} else {
error_log('Invalid attachment ID: ' . $attachment_id);
}
} else {
error_log('No file uploaded.');
}
}
} add_action('frm_after_create_entry', 'handle_formidable_img_upload', 10, 2);

 

 

Issues Encountered:
  1. No Featured Image in the Media Library: Despite the code running without errors, the image does not appear in the WordPress Media Library.
  2. Logs Indicate Success, But Image Is Missing: The log file indicates that the featured image was set successfully, but when I check the actual post, there’s no featured image displayed.
  3. Invalid URL/Attachment IDs: The log also shows “Invalid attachment ID” or “No file uploaded” errors intermittently.
What I've Tried:
  • I’ve ensured that the image field is correctly linked to the post.
  • I’ve tried adjusting the timing of the code execution within WordPress (e.g., different hooks) to no avail.
  • I’ve confirmed that the image files are correctly uploaded and accessible in the Formidable Forms entry.
Questions:
  1. Is there something I’m missing in the code that would ensure the image is properly set as the featured image?
  2. Are there any known issues with Formidable Forms' file upload handling that could be affecting this process?
  3. Is there a better method or recommended approach for setting a featured image using a Formidable Forms file upload?

Any guidance or suggestions would be greatly appreciated! Thanks in advance for your help!

1 Answers
Victor Font answered 3 weeks ago
Why do you have all of those WordPress includes for if exists 'media_handle_sideload' when there's no function of that name in your code? I also don't understand your variable naming conventions like "[ID X]". This is not something you'd find in Formidable. You say this code works but by all indications, it shouldn't fun at all without producing errors. Your priority is alo wrong. Priority for frm_after_create_entry is 30. You may be running it too early.
leena aikawaleena aikawa replied 3 weeks ago

Thank you so much for your help and insight! I wanted to clarify a couple of points regarding the code I initially posted. The functions included were there due to multiple errors encountered during previous attempts, which led me to keep them in as a precaution. Also, the placeholder IDs like [ID X] were just temporary markers for the actual IDs I was using in my code.

After applying your suggestion to change the priority for the frm_after_create_entry action to 30, everything started working perfectly. I can't express how grateful I am for your guidance—it's made a world of difference in resolving this issue.

Thanks again for your invaluable help!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right