$id,
'post_title' => $_POST['image-title'],
'post_content' => $_POST['image-alt'],
));
}
else {
$id = image_upload_post();
}
// if the input was invalid, redisplay the form with its current values
if ( is_wp_error($id) )
wp_iframe( 'image_upload_form', get_option('siteurl') . '/wp-admin/media-upload.php?type=image', $_POST, $id );
else {
image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url']);
}
}
}
// this returns html to include in the single image upload form when the async flash upload has finished
// i.e. show a thumb of the image, and include the attachment id as a hidden input
function async_image_callback($id) {
$thumb_url = wp_get_attachment_thumb_url($id);
if ( empty($thumb_url) )
$thumb_url = wp_mime_type_icon($id);
if ($thumb_url) {
$out = '
'
. ' '
. basename(wp_get_attachment_url($id)).'
';
}
else {
$out = '
'
. basename(wp_get_attachment_url($id)).'
';
}
$post = get_post($id);
$title = addslashes($post->post_title);
$alt = addslashes($post->post_content);
// populate the input fields with post data (which in turn comes from exif/iptc)
$out .= <<
EOF;
return $out;
}
add_filter('async_upload_image', 'async_image_callback');
// scale down the default size of an image so it's a better fit for the editor and theme
function image_constrain_size_for_editor($width, $height) {
// pick a reasonable default width for the image
// $content_width might be set in the theme's functions.php
if ( !empty($GLOBALS['content_width']) )
$max_width = $GLOBALS['content_width'];
else
$max_width = 500;
$max_width = apply_filters( 'editor_max_image_width', $max_width );
$max_height = apply_filters( 'editor_max_image_height', $max_width );
return wp_shrink_dimensions( $width, $height, $max_width, $max_height );
}
function image_send_to_editor($id, $alt, $title, $align, $url='') {
$img_src = wp_get_attachment_url($id);
$meta = wp_get_attachment_metadata($id);
$hwstring = '';
if ( isset($meta['width'], $meta['height']) ) {
list( $width, $height ) = image_constrain_size_for_editor( $meta['width'], $meta['height'] );
$hwstring = ' width="'.intval($width).'" height="'.intval($height).'"';
}
$html = '';
if ( $url )
$html = ''.$html.'';
$html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url );
media_send_to_editor($html);
}
function media_send_to_editor($html) {
?>
false);
$file = wp_handle_upload($_FILES['image-file'], $overrides);
if ( isset($file['error']) )
return new wp_error( 'upload_error', $file['error'] );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$post_title = trim($_POST['image-title']);
$post_content = trim($_POST['image-alt']);
$post_parent = intval($_POST['parent_post_id']);
// Construct the attachment array
$attachment = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_type' => 'attachment',
'post_parent' => $post_parent,
'post_mime_type' => $type,
'guid' => $url
);
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_parent);
if ( !is_wp_error($id) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
return $id;
}
// this handles the file upload POST itself, creating the attachment post
function media_handle_upload($file_id, $post_id, $post_data = array()) {
$overrides = array('test_form'=>false);
$file = wp_handle_upload($_FILES[$file_id], $overrides);
if ( isset($file['error']) )
return new wp_error( 'upload_error', $file['error'] );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\.[^.]+$/', '', basename($file));
$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']) )
$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 );
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_parent);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
return $id;
}
// wrap iframe content (produced by $content_func) in a doctype, html head/body etc
// any additional function args will be passed to content_func
function wp_iframe($content_func /* ... */) {
?>
>
› — WordPress
EOF;
echo $out;
}
add_action( 'media_buttons', 'media_buttons' );
function media_buttons_head() {
$siteurl = get_option('siteurl');
echo "\n";
}
add_action( 'admin_print_scripts', 'media_buttons_head' );
function media_admin_css() {
wp_admin_css('css/media');
}
add_action('media_upload_multimedia', 'multimedia_upload_handler');
add_action('media_upload_image', 'image_upload_handler');
add_action('admin_head_image_upload_form', 'media_admin_css');
function multimedia_upload_handler() {
if ( !current_user_can('upload_files') ) {
return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') );
}
if ( empty($_POST) ) {
// no button click, we're just displaying the form
wp_iframe( 'multimedia_upload_form' );
} elseif ( empty($_POST['upload-button']) ) {
// Insert multimedia button was clicked
check_admin_referer('multimedia-form');
if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
$post = $_post = get_post($attachment_id, ARRAY_A);
$post['post_content'] = $attachment['post_content'];
$post['post_title'] = $attachment['post_title'];
if ( $post != $_post )
wp_update_post($post);
if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t )
if ( isset($attachment[$t]) )
wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
}
media_send_to_editor('[gallery]');
} else {
// Upload File button was clicked
$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
wp_iframe( 'multimedia_upload_form' );
}
}
function get_multimedia_items( $post_id ) {
$attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\"");
if ( empty($attachments) )
return '';
foreach ( $attachments as $id => $attachment ) {
$output .= "\n