I have a front end post form where I take a file upload of an image. That image gets stored as a custom field which is easy enough to do. What I need to do though is store each unique image url for each user into an array in their usermeta. I want to do this so I can show them a list of those images they've already uploaded and give them the option of choosing one of those instead of uploading a new one.

So to elaborate I have a basic file upload choice in my form.

<input type="file" name="music_image" id="music_image"/>

Beneath this I need a check box select showing them the images of any others they have uploaded and somehow make it so if they choose a file they can't select an image an vice versa. And then I need to add the uploaded file to an array as usermeta on form submit. Awful explanation but I think it makes sense.

Here is what I use to activate the form.

 <?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
if ( ! function_exists( 'wp_handle_upload' )) { 
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter a game  title';
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
$tags = $_POST['post_tags'];

// Add the content of the form to $post as an array
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_category' => array(11),
    'tags_input'    => array($tags),
    'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' => fod_music  // Use a custom post type if you want to
$overrides = array( 'test_form' => false);
$uploaded_music = wp_handle_upload( $file['music_file'], $overrides );
$uploaded_music_art = wp_handle_upload( $file['music_image'], $overrides ); 
//save the new post and return its ID
$pid = wp_insert_post($new_post);
wp_redirect( get_permalink($pid)); 

do_action('wp_insert_post', 'wp_insert_post');
  • Why an array? Can you not have multiple user meta with the same name but different values and use that instead?
    – Tom J Nowell
    Aug 29, 2012 at 16:01
  • I assume you're using attachment IDs?
    – Tom J Nowell
    Aug 29, 2012 at 16:01
  • I don't know why an array honeslty lol. I need this so users can submit cover art for submitted music. To keep them from uploading the same album cover over and over Im trying to work out this solution if that helps shed light on things. Aug 29, 2012 at 16:04
  • 1
    I updated the post to show more of what I'm doing. Aug 29, 2012 at 16:07

1 Answer 1


Instead of saving the URL of the sideloaded file, save its ID. You can then attach post meta or a tax term to the new attachment indicating its album art.

That way your meta stores the ID of the current cover, and if you want a selection, just show attachments that are images with the associated postmeta indicating they belong to the current post. It will probably be enough to use the parent bit.

This gives you the extra flexibility of using image sizes. I'd advise you do the same for music files.

Use media_handle_sideload to handle your file upload, taken from http://core.trac.wordpress.org/browser/tags/3.4.1/wp-admin/includes/media.php:

258 /**
259  * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
260  *
261  * @since 2.6.0
262  *
263  * @param array $file_array Array similar to a {@link $_FILES} upload array
264  * @param int $post_id The post ID the media is associated with
265  * @param string $desc Description of the sideloaded file
266  * @param array $post_data allows you to overwrite some of the attachment
267  * @return int|object The ID of the attachment or a WP_Error on failure
268  */
269 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {

That should give you an attachment from your file upload, and attach it to the specified post. You can then do a selection by showing thumbnails of each cover using a radio selection, with the attachment ID of each cover image as the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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