• Hi guys,
    Need some help on this one. I’m trying to upload multiple image attachments via Front-End and save their IDs in custom fields.

    Right now only the first image gets saved as custom field (tds_shop_photo), but all of them are being uploaded to Media Libary. Need to be able to save all of them.

    Here’s the code (functions.php)

    function insert_attachment($file_handler,$post_id,$setthumb='false') {
        // check to make sure its a successful upload
        if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
        $attach_id = media_handle_upload( $file_handler, $post_id );
    
    	//$src = wp_get_attachment_image_src($attach_id,'full');
        if ($setthumb)
    				   update_post_meta($post_id,'tds_shop_photo',$attach_id); // shop photo
    				   update_post_meta($post_id,'tds_shop_logo',$attach_id); // shop logo
    				   update_post_meta($post_id,'tds_shop_cover_image',$attach_id); // shop cover image
        return $attach_id;
    }

    Front-End upload page

    /* IMAGE UPLOAD */
    	if ($_FILES) {
        foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file,$pid);
        // $newupload returns the attachment id of the file that
        // was just uploaded. Do whatever you want with that now.
        }
    	}

    Image upload form

    <fieldset class="images">
    <label for="images">Shop Photo</label>
    <input type="file" name="tds_shop_photo" id="tds_shop_photo" size="50" multiple>
    
    <label for="images">Shop Logo</label>
    <input type="file" name="tds_shop_logo" id="tds_shop_logo" size="50" multiple>

    <label for=”images”>Shop Cover Image</label>
    <input type=”file” name=”tds_shop_cover_image” id=”tds_shop_cover_image” size=”50″ multiple>
    </fieldset>

  • The topic ‘Upload Multiple image attachments and save them as custom fields via Front-End’ is closed to new replies.