Post image gallery

Home Forums Quform WordPress Post image gallery

This topic is: not resolved
  • This topic has 1 reply, 2 voices, and was last updated 3 years ago by Ally.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #32789
    lighflow
    Participant

    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;
    }

    #32790
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy