Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
Can you please give an edit containing the markup? –  whiteletters and blankspaces Nov 1 '13 at 16:44

1 Answer 1

up vote 1 down vote accepted

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.