Storing Attachment ID using media_handle_upload
-
I’ve created custom profile fields for users with a custom role. I’m using the media_handle_upload function in a custom themed profile update page accessible to the role. Uploads from the form are successful as three copies of the uploaded image are created in the current month’s uploads directory.
However, no matter what I’ve tried I cannot save the attachment id or access the attachment id to successfully get the attachment url to save one or the other via update_user_meta. Here’s the really simple bit I’m using from the Codex:
require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . 'wp-admin/includes/media.php' ); $attachment_id = media_handle_upload( 'profile_image_upload', $_POST['post_id'] ); //$attachment_url = wp_get_attachment_url($attachment_id); $attachment_url = wp_get_attachment_image_src($attachment_id, 'thumbnail' ); $user_thumb = $attachment_url[0]; if ( current_user_can( 'edit_user', $user_id ) ) update_user_meta( $user->ID, 'user_image', $user_thumb );
I’ve alternated between inserting $attachment_id, $attachment_url fetched by wp_get_attachment_url and and latterly by using wp_get_attachment_image_src to get the thumbnail.
It appears that all processing ceases when the file is uploaded as even echoing the assumed output returns blank.
Thanks for any help in getting either “attachment_id” or “attachment_url” as per the snippet above.
EDIT: MY form field sets post id to 0 as the image is not being attached to a post.
- The topic ‘Storing Attachment ID using media_handle_upload’ is closed to new replies.