Uploading Multiple Attachments From Front-End With A Description

The question:

I am working on a WordPress site that has a custom front-end submission form for adding in a post of content type, ‘sketchpad’. The form has a section that allows you to upload multiple images with a description field which is going to be used as an alt tag. What is an easy way for me to upload multiple attachments with accompanying descriptions from the front-end?

The code that currently catches the uploaded files is here:

// Make sure a user can edit posts (contributor level)
if (current_user_can('edit_posts'))
{
    global $post;

    // If we have files
    if ( $_FILES )
    {
        // Get the upload attachment files
        $files = $_FILES['upload_attachment'];

        foreach ($files['name'] as $key => $value)
        {
            if ($files['name'][$key])
            {
                $file = array(
                    'name' => $files['name'][$key],
                    'type' => $files['type'][$key],
                    'tmp_name' => $files['tmp_name'][$key],
                    'error' => $files['error'][$key],
                    'size' => $files['size'][$key]
                );

                $_FILES = array("upload_attachment" => $file);

                foreach ($_FILES as $file => $array)
                {
                    $newupload = insert_attachment($file,$post->ID);
                }
            }
        }
    }
}

This is the ‘insert attachment’ function that actually handles the uploaded files and inserts them:

function insert_attachment($file_handler, $post_id)
{
    // 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 );

    return $attach_id;
}

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I eventually worked this out. After a lot of thinking it dawned on me that attachments are merely posts assigned to the post_type of ‘attachment’ so then I soon discovered that attachments have a post_content field, as well as a post_title field. For my needs, I only needed the post_title attribute as my descriptions are small. The below code is what worked for me.

The $upload_id value is merely the attachment ID returned from my insert_attachment function.

// Make sure we have an attachment ID
if ($upload_id != 0)
{
    // Insert the upload description a.k.a post title
    $post_data = array();
    $post_data['ID'] = $upload_id;
    $post_data['post_title'] = $file_title;
    wp_update_post($post_data);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment