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.
custom-post-type-1
(for Form ID X)custom-post-type-2
(for Form ID A)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.
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);
Any guidance or suggestions would be greatly appreciated! Thanks in advance for your help!
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!
Please login or Register to submit your answer