Home › Forums › Quform WordPress › Post image gallery
- This topic has 1 reply, 2 voices, and was last updated 3 years, 7 months ago by Ally.
- AuthorPosts
- April 2, 2021 at 3:27 pm #32789lighflowParticipant
Hi,
I’m creating a new post using the code below, everything works except for the images I’m trying to pass to the post image gallery. I have all images in media library but I can’t see them in post image gallery.
Could you help, please?
add_filter(‘quform_post_process_5’, function (array $result, Quform_Form $form) {
$title = $form->getValue(‘quform_5_8’);
$content = $form->getValue(‘quform_5_18’);$post = array(
‘post_title’ => $title,
‘post_content’ => $content,
‘post_type’ => ‘ibo’,
‘post_status’ => ‘draft’
);$postId = wp_insert_post($post);
// Add featured image
$file = $form->getValue(‘quform_5_7’);if (isset($file[0])) {
$file = $file[0];
$filename = $file[‘name’];
$path = $file[‘path’];$wpFiletype = wp_check_filetype($filename);
$attachment = array(
‘guid’ => $file[‘url’],
‘post_mime_type’ => $wpFiletype[‘type’],
‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, $filename),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);$attachId = wp_insert_attachment($attachment, $path, $postId);
require_once ABSPATH . ‘wp-admin/includes/image.php’;
$attachData = wp_generate_attachment_metadata($attachId, $path);
wp_update_attachment_metadata($attachId, $attachData);set_post_thumbnail($postId, $attachId);
}// Create gallery
$attachmentIds = array();
$files = $form->getValue(‘quform_5_29’);foreach($files as $file) {
$attachId = my_add_file_attachment($file);
$attachmentIds[] = $attachId;
}return $result;
}, 10, 2);function my_add_file_attachment($file, $parentPostId = 0)
{
require_once ABSPATH . ‘wp-admin/includes/image.php’;$filename = $file[‘name’];
$path = $file[‘path’];$wpFiletype = wp_check_filetype($filename);
$attachment = array(
‘guid’ => $file[‘url’],
‘post_mime_type’ => $wpFiletype[‘type’],
‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, $filename),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);$attachId = wp_insert_attachment($attachment, $path, $parentPostId);
$attachData = wp_generate_attachment_metadata($attachId, $path);
wp_update_attachment_metadata($attachId, $attachData);return $attachId;
}April 6, 2021 at 1:01 pm #32790AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.