media_handle_upload [ WordPress Function ]
media_handle_upload ( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) )
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
Similar Functions: media_handle_sideload, wp_handle_upload, wp_import_handle_upload, media_upload_form, media_upload_tabs
{@internal Missing Short Description}}
This handles the file upload POST itself, creating the attachment post.
Source
<?php
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
$time = current_time('mysql');
if ( $post = get_post($post_id) ) {
if ( substr( $post->post_date, 0, 4 ) > 0 )
$time = $post->post_date;
}
$name = $_FILES[$file_id]['name'];
$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
if ( isset($file['error']) )
return new WP_Error( 'upload_error', $file['error'] );
$name_parts = pathinfo($name);
$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = $name;
$content = '';
// use image exif/iptc data for title and caption defaults if possible
if ( $image_meta = @wp_read_image_metadata($file) ) {
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
$title = $image_meta['title'];
if ( trim( $image_meta['caption'] ) )
$content = $image_meta['caption'];
}
// Construct the attachment array
$attachment = array_merge( array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content,
), $post_data );
// This should never be set as it would then overwrite an existing attachment.
if ( isset( $attachment['ID'] ) )
unset( $attachment['ID'] );
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
return $id;
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- Function Reference/media handle upload « WordPress Codex
Usage. <?php media_handle_upload( $file_id, $post_id, $post_data, $overrides ); ?> Parameters. $file_id: (string) (required) Index into the {@link $_FILES} ...
codex.wordpress.org - WordPress › Support » Using media_handle_upload from the Front ...
Using media_handle_upload from the Front-End and Attaching Media to a ... am going wrong here and the code is amazingly brief on media_handle_upload so ...
wordpress.org - php - using upload image with media_handle_upload .. but ! - Stack ...
Perhaps you'll find something of relevance in the examples given here regarding the media uploader.
stackoverflow.com - 3.0 - using upload image with media_handle_upload .. but ...
Dec 20, 2010 ... I'm tring to upload image in wordpress by using some function .. I found the way to upload images but there are one problem .. when the user ...
wordpress.stackexchange.com
User discussions [ wordpress.org ]
- Zambrano Sergio on "Automatically move uploaded files to uploads directory"
- idobi on "Automatically move uploaded files to uploads directory"
- touchy finger on "Automatically move uploaded files to uploads directory"
- touchy finger on "Automatically move uploaded files to uploads directory"
- acsnaterse on "Automatically move uploaded files to uploads directory"
- Joost de Valk on "Automatically move uploaded files to uploads directory"
- acsnaterse on "Automatically move uploaded files to uploads directory"
- idealien on "Custom Post Type / File Upload"