3 function media_upload_tabs() {
4 $_default_tabs = array(
5 'type' => __('Choose File'), // handler action suffix => tab text
6 'gallery' => __('Gallery'),
7 'library' => __('Media Library'),
10 return apply_filters('media_upload_tabs', $_default_tabs);
13 function update_gallery_tab($tabs) {
15 if ( !isset($_REQUEST['post_id']) ) {
16 unset($tabs['gallery']);
19 if ( intval($_REQUEST['post_id']) )
20 $attachments = intval($wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $_REQUEST['post_id'])));
22 $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
26 add_filter('media_upload_tabs', 'update_gallery_tab');
28 function the_media_upload_tabs() {
29 $tabs = media_upload_tabs();
31 if ( !empty($tabs) ) {
32 echo "<ul id='sidemenu'>\n";
33 if ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
34 $current = $_GET['tab'];
36 $keys = array_keys($tabs);
37 $current = array_shift($keys);
39 foreach ( $tabs as $callback => $text ) {
41 if ( $current == $callback )
42 $class = " class='current'";
43 $href = add_query_arg(array('tab'=>$callback, 's'=>false, 'paged'=>false, 'post_mime_type'=>false, 'm'=>false));
44 $link = "<a href='$href'$class>$text</a>";
45 echo "\t<li id='tab-$callback'>$link</li>\n";
51 function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') {
53 $html = get_image_tag($id, $alt, $title, $align, $size);
55 $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
58 $html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
60 $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
65 function media_send_to_editor($html) {
67 <script type="text/javascript">
69 top.send_to_editor('<?php echo addslashes($html); ?>');
77 // this handles the file upload POST itself, creating the attachment post
78 function media_handle_upload($file_id, $post_id, $post_data = array()) {
79 $overrides = array('test_form'=>false);
80 $file = wp_handle_upload($_FILES[$file_id], $overrides);
82 if ( isset($file['error']) )
83 return new wp_error( 'upload_error', $file['error'] );
86 $type = $file['type'];
87 $file = $file['file'];
88 $title = preg_replace('/\.[^.]+$/', '', basename($file));
91 // use image exif/iptc data for title and caption defaults if possible
92 if ( $image_meta = @wp_read_image_metadata($file) ) {
93 if ( trim($image_meta['title']) )
94 $title = $image_meta['title'];
95 if ( trim($image_meta['caption']) )
96 $content = $image_meta['caption'];
99 // Construct the attachment array
100 $attachment = array_merge( array(
101 'post_mime_type' => $type,
103 'post_parent' => $post_id,
104 'post_title' => $title,
105 'post_content' => $content,
109 $id = wp_insert_attachment($attachment, $file, $post_parent);
110 if ( !is_wp_error($id) ) {
111 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
119 // wrap iframe content (produced by $content_func) in a doctype, html head/body etc
120 // any additional function args will be passed to content_func
121 function wp_iframe($content_func /* ... */) {
123 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
124 <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
126 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
127 <title><?php bloginfo('name') ?> › <?php _e('Uploads'); ?> — <?php _e('WordPress'); ?></title>
129 wp_admin_css( 'css/global' );
131 wp_admin_css( 'css/colors' );
133 <script type="text/javascript">
135 function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}else{ var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}}
139 do_action('admin_print_scripts');
140 do_action('admin_head');
141 if ( is_string($content_func) )
142 do_action( "admin_head_{$content_func}" );
145 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?>>
147 $args = func_get_args();
148 $args = array_slice($args, 1);
149 call_user_func_array($content_func, $args);
156 function media_buttons() {
157 global $post_ID, $temp_ID;
158 $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
159 $context = apply_filters('media_buttons_context', __('Add media: %s'));
160 $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
161 $media_title = __('Add Media');
162 $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image");
163 $image_title = __('Add an Image');
164 $video_upload_iframe_src = apply_filters('video_upload_iframe_src', "$media_upload_iframe_src&type=video");
165 $video_title = __('Add Video');
166 $audio_upload_iframe_src = apply_filters('audio_upload_iframe_src', "$media_upload_iframe_src&type=audio");
167 $audio_title = __('Add Audio');
170 <a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox" title='$image_title'><img src='images/media-button-image.gif' alt='$image_title' /></a>
171 <a href="{$video_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox" title='$video_title'><img src='images/media-button-video.gif' alt='$video_title' /></a>
172 <a href="{$audio_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox" title='$audio_title'><img src='images/media-button-music.gif' alt='$audio_title' /></a>
173 <a href="{$media_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox" title='$media_title'><img src='images/media-button-other.gif' alt='$media_title' /></a>
176 printf($context, $out);
178 add_action( 'media_buttons', 'media_buttons' );
180 function media_buttons_head() {
181 $siteurl = get_option('siteurl');
182 echo "<style type='text/css' media='all'>
183 @import '{$siteurl}/wp-includes/js/thickbox/thickbox.css?1';
185 background-color: #222222;
188 div#TB_title a, div#TB_title a:visited {
194 add_action( 'admin_print_scripts', 'media_buttons_head' );
196 function media_admin_css() {
197 wp_admin_css('css/media');
200 add_action('media_upload_media', 'media_upload_handler');
202 function media_upload_form_handler() {
203 check_admin_referer('media-form');
205 if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
206 $post = $_post = get_post($attachment_id, ARRAY_A);
207 if ( isset($attachment['post_content']) )
208 $post['post_content'] = $attachment['post_content'];
209 if ( isset($attachment['post_title']) )
210 $post['post_title'] = $attachment['post_title'];
211 if ( isset($attachment['post_excerpt']) )
212 $post['post_excerpt'] = $attachment['post_excerpt'];
214 $post = apply_filters('attachment_fields_to_save', $post, $attachment);
216 if ( isset($post['errors']) ) {
217 $errors[$attachment_id] = $post['errors'];
218 unset($post['errors']);
221 if ( $post != $_post )
222 wp_update_post($post);
224 foreach ( get_attachment_taxonomies($post) as $t )
225 if ( isset($attachment[$t]) )
226 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
229 if ( isset($_POST['insert-gallery']) )
230 return media_send_to_editor('[gallery]');
232 if ( isset($_POST['send']) ) {
233 $keys = array_keys($_POST['send']);
234 $send_id = (int) array_shift($keys);
235 $attachment = $_POST['attachments'][$send_id];
236 $html = $attachment['post_title'];
237 if ( !empty($attachment['url']) ) {
238 if ( strpos($attachment['url'], 'attachment_id') || false !== strpos($attachment['url'], get_permalink($_POST['post_id'])) )
239 $rel = " rel='attachment wp-att-".attribute_escape($send_id)."'";
240 $html = "<a href='{$attachment['url']}'$rel>$html</a>";
242 $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
243 return media_send_to_editor($html);
249 function media_upload_image() {
250 if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
251 // Upload File button was clicked
252 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
254 if ( is_wp_error($id) ) {
255 $errors['upload_error'] = $id;
260 if ( !empty($_POST['insertonlybutton']) ) {
261 $src = $_POST['insertonly']['src'];
262 if ( !empty($src) && !strpos($src, '://') )
263 $src = "http://$src";
264 $alt = attribute_escape($_POST['insertonly']['alt']);
265 if ( isset($_POST['insertonly']['align']) ) {
266 $align = attribute_escape($_POST['insertonly']['align']);
267 $class = " class='align$align'";
270 $html = "<img src='$src' alt='$alt'$class />";
271 return media_send_to_editor($html);
274 if ( !empty($_POST) ) {
275 $return = media_upload_form_handler();
277 if ( is_string($return) )
279 if ( is_array($return) )
283 if ( isset($_POST['save']) )
284 $errors['upload_notice'] = __('Saved.');
286 return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
289 function media_upload_audio() {
290 if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
291 // Upload File button was clicked
292 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
294 if ( is_wp_error($id) ) {
295 $errors['upload_error'] = $id;
300 if ( !empty($_POST['insertonlybutton']) ) {
301 $href = $_POST['insertonly']['href'];
302 if ( !empty($href) && !strpos($href, '://') )
303 $href = "http://$href";
304 $title = attribute_escape($_POST['insertonly']['title']);
306 $title = basename($href);
307 if ( !empty($title) && !empty($href) )
308 $html = "<a href='$href' >$title</a>";
309 return media_send_to_editor($html);
312 if ( !empty($_POST) ) {
313 $return = media_upload_form_handler();
315 if ( is_string($return) )
317 if ( is_array($return) )
321 if ( isset($_POST['save']) )
322 $errors['upload_notice'] = __('Saved.');
324 return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
327 function media_upload_video() {
328 if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
329 // Upload File button was clicked
330 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
332 if ( is_wp_error($id) ) {
333 $errors['upload_error'] = $id;
338 if ( !empty($_POST['insertonlybutton']) ) {
339 $href = $_POST['insertonly']['href'];
340 if ( !empty($href) && !strpos($href, '://') )
341 $href = "http://$href";
342 $title = attribute_escape($_POST['insertonly']['title']);
344 $title = basename($href);
345 if ( !empty($title) && !empty($href) )
346 $html = "<a href='$href' >$title</a>";
347 return media_send_to_editor($html);
350 if ( !empty($_POST) ) {
351 $return = media_upload_form_handler();
353 if ( is_string($return) )
355 if ( is_array($return) )
359 if ( isset($_POST['save']) )
360 $errors['upload_notice'] = __('Saved.');
362 return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
365 function media_upload_file() {
366 if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
367 // Upload File button was clicked
368 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
370 if ( is_wp_error($id) ) {
371 $errors['upload_error'] = $id;
376 if ( !empty($_POST['insertonlybutton']) ) {
377 $href = $_POST['insertonly']['href'];
378 if ( !empty($href) && !strpos($href, '://') )
379 $href = "http://$href";
380 $title = attribute_escape($_POST['insertonly']['title']);
382 $title = basename($href);
383 if ( !empty($title) && !empty($href) )
384 $html = "<a href='$href' >$title</a>";
385 return media_send_to_editor($html);
388 if ( !empty($_POST) ) {
389 $return = media_upload_form_handler();
391 if ( is_string($return) )
393 if ( is_array($return) )
397 if ( isset($_POST['save']) )
398 $errors['upload_notice'] = __('Saved.');
400 return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
403 function media_upload_gallery() {
404 if ( !empty($_POST) ) {
405 $return = media_upload_form_handler();
407 if ( is_string($return) )
409 if ( is_array($return) )
413 return wp_iframe( 'media_upload_gallery_form', $errors );
416 function media_upload_library() {
417 if ( !empty($_POST) ) {
418 $return = media_upload_form_handler();
420 if ( is_string($return) )
422 if ( is_array($return) )
426 return wp_iframe( 'media_upload_library_form', $errors );
429 function image_attachment_fields_to_edit($form_fields, $post) {
430 if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
431 $form_fields['post_title']['required'] = true;
432 $form_fields['post_excerpt']['label'] = __('Caption');
433 $form_fields['post_excerpt']['helps'][] = __('Alternate text, e.g. "The Mona Lisa"');
435 $form_fields['post_content']['label'] = __('Description');
437 $thumb = wp_get_attachment_thumb_url($post->ID);
439 $form_fields['align'] = array(
440 'label' => __('Alignment'),
443 <input type='radio' name='attachments[$post->ID][align]' id='image-align-none-$post->ID' value='none' checked='checked' />
444 <label for='image-align-none-$post->ID' class='align image-align-none-label'>" . __('None') . "</label>
445 <input type='radio' name='attachments[$post->ID][align]' id='image-align-left-$post->ID' value='left' />
446 <label for='image-align-left-$post->ID' class='align image-align-left-label'>" . __('Left') . "</label>
447 <input type='radio' name='attachments[$post->ID][align]' id='image-align-center-$post->ID' value='center' />
448 <label for='image-align-center-$post->ID' class='align image-align-center-label'>" . __('Center') . "</label>
449 <input type='radio' name='attachments[$post->ID][align]' id='image-align-right-$post->ID' value='right' />
450 <label for='image-align-right-$post->ID' class='align image-align-right-label'>" . __('Right') . "</label>\n",
452 $form_fields['image-size'] = array(
453 'label' => __('Size'),
456 " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' />
457 <label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label>
458 " : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' />
459 <label for='image-size-medium-$post->ID'>" . __('Medium') . "</label>
460 <input type='radio' name='attachments[$post->ID][image-size]' id='image-size-full-$post->ID' value='full' />
461 <label for='image-size-full-$post->ID'>" . __('Full size') . "</label>",
467 add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2);
469 function media_single_attachment_fields_to_edit( $form_fields, $post ) {
470 unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
474 function image_attachment_fields_to_save($post, $attachment) {
475 if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
476 if ( strlen(trim($post['post_title'])) == 0 ) {
477 $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
478 $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
485 add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
487 function image_media_send_to_editor($html, $attachment_id, $attachment) {
488 $post =& get_post($attachment_id);
489 if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
490 $url = $attachment['url'];
492 if ( isset($attachment['align']) )
493 $align = $attachment['align'];
497 if ( !empty($attachment['image-size']) )
498 $size = $attachment['image-size'];
502 $rel = ( $url == get_attachment_link($attachment_id) );
504 return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size);
510 add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
512 function get_attachment_fields_to_edit($post, $errors = null) {
514 $post =& get_post($post);
515 if ( is_array($post) )
516 $post = (object) $post;
518 $edit_post = sanitize_post($post, 'edit');
519 $file = wp_get_attachment_url($post->ID);
520 $link = get_attachment_link($post->ID);
522 $form_fields = array(
523 'post_title' => array(
524 'label' => __('Title'),
525 'value' => $edit_post->post_title,
527 'post_excerpt' => array(
528 'label' => __('Caption'),
529 'value' => $edit_post->post_excerpt,
531 'post_content' => array(
532 'label' => __('Description'),
533 'value' => $edit_post->post_content,
534 'input' => 'textarea',
537 'label' => __('Link URL'),
540 <input type='text' name='attachments[$post->ID][url]' value='" . attribute_escape($file) . "' /><br />
541 <button type='button' class='button url-$post->ID' value=''>" . __('None') . "</button>
542 <button type='button' class='button url-$post->ID' value='" . attribute_escape($file) . "'>" . __('File URL') . "</button>
543 <button type='button' class='button url-$post->ID' value='" . attribute_escape($link) . "'>" . __('Post URL') . "</button>
544 <script type='text/javascript'>
545 jQuery('button.url-$post->ID').bind('click', function(){jQuery(this).siblings('input').val(this.value);});
547 'helps' => __('Enter a link URL or click above for presets.'),
551 foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
552 $t = (array) get_taxonomy($taxonomy);
553 if ( empty($t['label']) )
554 $t['label'] = $taxonomy;
555 if ( empty($t['args']) )
556 $t['args'] = array();
558 $terms = get_object_term_cache($post->ID, $taxonomy);
560 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
564 foreach ( $terms as $term )
565 $values[] = $term->name;
566 $t['value'] = join(', ', $values);
568 $form_fields[$taxonomy] = $t;
571 // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
572 // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
573 $form_fields = array_merge_recursive($form_fields, (array) $errors);
575 $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
580 function get_media_items( $post_id, $errors ) {
582 $post = get_post($post_id);
583 if ( $post && $post->post_type == 'attachment' )
584 $attachments = array($post->ID => $post);
586 $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=menu_order ASC, ID&order=DESC");
588 if ( is_array($GLOBALS['wp_the_query']->posts) )
589 foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
590 $attachments[$attachment->ID] = $attachment;
593 if ( empty($attachments) )
596 foreach ( $attachments as $id => $attachment )
597 if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
598 $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
603 function get_media_item( $attachment_id, $args = null ) {
604 $default_args = array( 'errors' => null, 'send' => true, 'delete' => true, 'toggle' => true );
605 $args = wp_parse_args( $args, $default_args );
606 extract( $args, EXTR_SKIP );
608 global $post_mime_types;
609 if ( ( $attachment_id = intval($attachment_id) ) && $thumb_url = get_attachment_icon_src( $attachment_id ) )
610 $thumb_url = $thumb_url[0];
614 $title_label = __('Title');
615 $description_label = __('Description');
616 $tags_label = __('Tags');
618 $toggle_on = __('Show');
619 $toggle_off = __('Hide');
621 $post = get_post($attachment_id);
623 $filename = basename($post->guid);
624 $title = attribute_escape($post->post_title);
625 $description = attribute_escape($post->post_content);
626 if ( $_tags = get_the_tags($attachment_id) ) {
627 foreach ( $_tags as $tag )
628 $tags[] = $tag->name;
629 $tags = attribute_escape(join(', ', $tags));
632 if ( isset($post_mime_types) ) {
633 $keys = array_keys(wp_match_mime_types(array_keys($post_mime_types), $post->post_mime_type));
634 $type = array_shift($keys);
635 $type = "<input type='hidden' id='type-of-$attachment_id' value='" . attribute_escape( $type ) . "' />";
638 $form_fields = get_attachment_fields_to_edit($post, $errors);
641 $class = empty($errors) ? 'startclosed' : 'startopen';
643 <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
644 <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
646 $class = 'form-table';
650 $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
655 <div class='filename new'>$display_title</div>
656 <table class='slidetoggle describe $class'>
657 <thead class='media-item-info'>
659 <td class='A1B1' rowspan='4'><img class='thumbnail' src='$thumb_url' alt='' /></td>
662 <tr><td>$post->post_mime_type</td></tr>
663 <tr><td>" . mysql2date($post->post_date, get_option('time_format')) . "</td></tr>
664 <tr><td>" . apply_filters('media_meta', '', $post) . "</td></tr>
672 'extra_rows' => array(),
675 $delete_href = wp_nonce_url("post.php?action=delete-post&post=$attachment_id", 'delete-post_' . $attachment_id);
677 $send = "<input type='submit' class='button' name='send[$attachment_id]' value='" . attribute_escape( __( 'Insert into Post' ) ) . "' />";
679 $delete = "<a href='$delete_href' id='del[$attachment_id]' disabled='disabled' class='delete'>" . __('Delete') . "</button>";
680 if ( ( $send || $delete ) && !isset($form_fields['buttons']) )
681 $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $delete</td></tr>\n");
683 $hidden_fields = array();
685 foreach ( $form_fields as $id => $field ) {
689 if ( !empty($field['tr']) ) {
690 $item .= $field['tr'];
694 $field = array_merge($defaults, $field);
695 $name = "attachments[$attachment_id][$id]";
697 if ( $field['input'] == 'hidden' ) {
698 $hidden_fields[$name] = $field['value'];
702 $required = $field['required'] ? '<abbr title="required" class="required">*</abbr>' : '';
704 $class .= $field['required'] ? ' form-required' : '';
706 $item .= "\t\t<tr class='$class'>\n\t\t\t<th valign='top' scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}</span><span class='alignright'>$required</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
707 if ( !empty($field[$field['input']]) )
708 $item .= $field[$field['input']];
709 elseif ( $field['input'] == 'textarea' ) {
710 $item .= "<textarea type='text' id='$name' name='$name'>" . attribute_escape( $field['value'] ) . "</textarea>";
712 $item .= "<input type='text' id='$name' name='$name' value='" . attribute_escape( $field['value'] ) . "' />";
714 if ( !empty($field['helps']) )
715 $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique((array) $field['helps']) ) . '</p>';
716 $item .= "</td>\n\t\t</tr>\n";
718 $extra_rows = array();
720 if ( !empty($field['errors']) )
721 foreach ( array_unique((array) $field['errors']) as $error )
722 $extra_rows['error'][] = $error;
724 if ( !empty($field['extra_rows']) )
725 foreach ( $field['extra_rows'] as $class => $rows )
726 foreach ( (array) $rows as $html )
727 $extra_rows[$class][] = $html;
729 foreach ( $extra_rows as $class => $rows )
730 foreach ( $rows as $html )
731 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
734 if ( !empty($form_fields['_final']) )
735 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
736 $item .= "\t</tbody>\n";
737 $item .= "\t</table>\n";
739 foreach ( $hidden_fields as $name => $value )
740 $item .= "\t<input type='hidden' name='$name' id='$name' value='" . attribute_escape( $value ) . "' />\n";
745 function media_upload_header() {
747 <script type="text/javascript">post_id = <?php echo intval($_REQUEST['post_id']); ?>;</script>
748 <div id="media-upload-header">
749 <?php the_media_upload_tabs(); ?>
754 function media_upload_form( $errors = null ) {
757 $flash_action_url = get_option('siteurl') . "/wp-admin/async-upload.php";
759 // If Mac and mod_security, no Flash. :(
761 if ( false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac') && apache_mod_loaded('mod_security') )
764 $flash = apply_filters('flash_uploader', $flash);
765 $post_id = intval($_REQUEST['post_id']);
768 <input type='hidden' name='post_id' value='<?php echo (int) $post_id; ?>' />
769 <div id="media-upload-notice">
770 <?php if (isset($errors['upload_notice']) ) { ?>
771 <?php echo $errors['upload_notice']; ?>
774 <div id="media-upload-error">
775 <?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
776 <?php echo $errors['upload_error']->get_error_message(); ?>
779 <?php if ( $flash ) : ?>
780 <script type="text/javascript">
783 swfu = new SWFUpload({
784 upload_url : "<?php echo attribute_escape( $flash_action_url ); ?>",
785 flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>",
786 file_post_name: "async-upload",
787 file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
789 "post_id" : "<?php echo $post_id; ?>",
790 "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
791 "type" : "<?php echo $type; ?>",
792 "tab" : "<?php echo $tab; ?>",
795 file_size_limit : "<?php echo wp_max_upload_size(); ?>b",
796 swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
797 degraded_element_id : "html-upload-ui", // when swfupload is unavailable
798 file_dialog_start_handler : fileDialogStart,
799 file_queued_handler : fileQueued,
800 upload_start_handler : uploadStart,
801 upload_progress_handler : uploadProgress,
802 upload_error_handler : uploadError,
803 upload_success_handler : uploadSuccess,
804 upload_complete_handler : uploadComplete,
805 file_queue_error_handler : fileQueueError,
806 file_dialog_complete_handler : fileDialogComplete,
810 $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
816 <div id="flash-upload-ui">
817 <p><input id="flash-browse-button" type="button" value="<?php echo attribute_escape( __( 'Choose files to upload' ) ); ?>" class="button" /></p>
818 <p><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
821 <?php endif; // $flash ?>
823 <div id="html-upload-ui">
825 <input type="file" name="async-upload" id="async-upload" /> <input type="submit" class="button" name="html-upload" value="<?php echo attribute_escape(__('Upload')); ?>" /> <a href="#" onClick="return top.tb_remove();"><?php _e('Cancel'); ?></a>
827 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
829 <?php if ( is_lighttpd_before_150() ): ?>
830 <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please upgrade to lighttpd 1.5.'); ?></p>
836 function media_upload_type_form($type = 'file', $errors = null, $id = null) {
837 media_upload_header();
839 $post_id = intval($_REQUEST['post_id']);
841 $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type=$type&tab=type&post_id=$post_id";
843 $callback = "type_form_$type";
846 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
847 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
848 <?php wp_nonce_field('media-form'); ?>
849 <h3><?php _e('From Computer'); ?></h3>
850 <?php media_upload_form( $errors ); ?>
852 <script type="text/javascript">
855 var preloaded = $(".media-item.preloaded");
856 if ( preloaded.length > 0 ) {
857 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
863 <?php if ( $id && !is_wp_error($id) ) : ?>
864 <div id="media-items">
865 <?php echo get_media_items( $id, $errors ); ?>
867 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
869 <?php elseif ( is_callable($callback) ) : ?>
871 <div class="media-blank">
872 <p style="text-align:center"><?php _e('— OR —'); ?></p>
873 <h3><?php _e('From URL'); ?></h3>
876 <div id="media-items">
877 <div class="media-item media-blank">
878 <?php echo call_user_func($callback); ?>
881 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
886 function media_upload_gallery_form($errors) {
887 media_upload_header();
889 $post_id = intval($_REQUEST['post_id']);
891 $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type={$GLOBALS['type']}&tab=gallery&post_id=$post_id";
895 <script type="text/javascript">
898 var preloaded = $(".media-item.preloaded");
899 if ( preloaded.length > 0 ) {
900 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
907 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
908 <?php wp_nonce_field('media-form'); ?>
909 <?php //media_upload_form( $errors ); ?>
911 <div id="media-items">
912 <?php echo get_media_items($post_id, $errors); ?>
914 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
915 <input type="submit" class="button insert-gallery" name="insert-gallery" value="<?php echo attribute_escape( __( 'Insert gallery into post' ) ); ?>" />
916 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
917 <input type="hidden" name="type" value="<?php echo attribute_escape( $GLOBALS['type'] ); ?>" />
918 <input type="hidden" name="tab" value="<?php echo attribute_escape( $GLOBALS['tab'] ); ?>" />
923 function media_upload_library_form($errors) {
924 global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
926 media_upload_header();
928 $post_id = intval($_REQUEST['post_id']);
930 $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type={$GLOBALS['type']}&tab=library&post_id=$post_id";
932 $_GET['paged'] = intval($_GET['paged']);
933 if ( $_GET['paged'] < 1 )
935 $start = ( $_GET['paged'] - 1 ) * 10;
938 add_filter( 'post_limits', $limit_filter = create_function( '$a', "return 'LIMIT $start, 10';" ) );
940 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
944 <form id="filter" action="" method="get">
945 <input type="hidden" name="type" value="<?php echo attribute_escape( $type ); ?>" />
946 <input type="hidden" name="tab" value="<?php echo attribute_escape( $tab ); ?>" />
947 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
948 <input type="hidden" name="post_mime_type" value="<?php echo attribute_escape( $_GET['post_mime_type'] ); ?>" />
950 <div id="search-filter">
951 <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>" />
952 <input type="submit" value="<?php echo attribute_escape( __( 'Search Media' ) ); ?>" class="button" />
956 <ul class="subsubsub">
958 $type_links = array();
959 $_num_posts = (array) wp_count_attachments();
960 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
961 foreach ( $matches as $_type => $reals )
962 foreach ( $reals as $real )
963 $num_posts[$_type] += $_num_posts[$real];
964 // If available type specified by media button clicked, filter by that type
965 if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
966 $_GET['post_mime_type'] = $type;
967 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
969 if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
970 $class = ' class="current"';
971 $type_links[] = "<li><a href='" . add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false)) . "'$class>".__('All Types')."</a>";
972 foreach ( $post_mime_types as $mime_type => $label ) {
975 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
978 if ( wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
979 $class = ' class="current"';
981 $type_links[] = "<li><a href='" . add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false)) . "'$class>" . sprintf(__ngettext($label[2][0], $label[2][1], $num_posts[$mime_type]), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
983 echo implode(' | </li>', $type_links) . '</li>';
989 <div class="tablenav">
992 $page_links = paginate_links( array(
993 'base' => add_query_arg( 'paged', '%#%' ),
995 'total' => ceil($wp_query->found_posts / 10),
996 'current' => $_GET['paged']
1000 echo "<div class='tablenav-pages'>$page_links</div>";
1003 <div class="alignleft">
1006 $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
1008 $arc_result = $wpdb->get_results( $arc_query );
1010 $month_count = count($arc_result);
1012 if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
1014 <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
1016 foreach ($arc_result as $arc_row) {
1017 if ( $arc_row->yyear == 0 )
1019 $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
1021 if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] )
1022 $default = ' selected="selected"';
1026 echo "<option$default value='" . attribute_escape( $arc_row->yyear . $arc_row->mmonth ) . "'>";
1027 echo wp_specialchars( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
1034 <input type="submit" id="post-query-submit" value="<?php echo attribute_escape( __( 'Filter »' ) ); ?>" class="button-secondary" />
1038 <br class="clear" />
1042 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="library-form">
1044 <?php wp_nonce_field('media-form'); ?>
1045 <?php //media_upload_form( $errors ); ?>
1047 <script type="text/javascript">
1050 var preloaded = $(".media-item.preloaded");
1051 if ( preloaded.length > 0 ) {
1052 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1059 <div id="media-items">
1060 <?php echo get_media_items(null, $errors); ?>
1062 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
1063 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1068 function type_form_image() {
1070 <table class="describe"><tbody>
1072 <th valign="top" scope="row" class="label">
1073 <span class="alignleft"><label for="insertonly[src]">' . __('Image URL') . '</label></span>
1074 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1076 <td class="field"><input id="insertonly[src]" name="insertonly[src]" value="" type="text"></td>
1079 <th valign="top" scope="row" class="label">
1080 <span class="alignleft"><label for="insertonly[alt]">' . __('Description') . '</label></span>
1081 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1083 <td class="field"><input id="insertonly[alt]" name="insertonly[alt]" value="" type="text"></td>
1085 <tr><td></td><td class="help">' . __('Alternate text, e.g. "The Mona Lisa"') . '</td></tr>
1087 <th valign="top" scope="row" class="label"><label for="insertonly[align]">' . __('Alignment') . '</label></th>
1089 <input name="insertonly[align]" id="image-align-none-0" value="none" type="radio" checked="checked" />
1090 <label for="image-align-none-0" class="align image-align-none-label">' . __('None') . '</label>
1091 <input name="insertonly[align]" id="image-align-left-0" value="left" type="radio" />
1092 <label for="image-align-left-0" class="align image-align-left-label">' . __('Left') . '</label>
1093 <input name="insertonly[align]" id="image-align-center-0" value="center" type="radio" />
1094 <label for="image-align-center-0" class="align image-align-center-label">' . __('Center') . '</label>
1095 <input name="insertonly[align]" id="image-align-right-0" value="right" type="radio" />
1096 <label for="image-align-right-0" class="align image-align-right-label">' . __('Right') . '</label>
1102 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
1109 function type_form_audio() {
1111 <table class="describe"><tbody>
1113 <th valign="top" scope="row" class="label">
1114 <span class="alignleft"><label for="insertonly[href]">' . __('Audio File URL') . '</label></span>
1115 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1117 <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
1120 <th valign="top" scope="row" class="label">
1121 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
1122 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1124 <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
1126 <tr><td></td><td class="help">' . __('Link text, e.g. "Still Alive by Jonathan Coulton"') . '</td></tr>
1130 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
1137 function type_form_video() {
1139 <table class="describe"><tbody>
1141 <th valign="top" scope="row" class="label">
1142 <span class="alignleft"><label for="insertonly[href]">' . __('Video URL') . '</label></span>
1143 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1145 <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
1148 <th valign="top" scope="row" class="label">
1149 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
1150 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1152 <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
1154 <tr><td></td><td class="help">' . __('Link text, e.g. "Lucy on YouTube"') . '</td></tr>
1158 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
1165 function type_form_file() {
1167 <table class="describe"><tbody>
1169 <th valign="top" scope="row" class="label">
1170 <span class="alignleft"><label for="insertonly[href]">' . __('URL') . '</label></span>
1171 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1173 <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
1176 <th valign="top" scope="row" class="label">
1177 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
1178 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
1180 <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
1182 <tr><td></td><td class="help">' . __('Link text, e.g. "Ransom Demands (PDF)"') . '</td></tr>
1186 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
1193 add_filter('async_upload_image', 'get_media_item', 10, 2);
1194 add_filter('async_upload_audio', 'get_media_item', 10, 2);
1195 add_filter('async_upload_video', 'get_media_item', 10, 2);
1196 add_filter('async_upload_file', 'get_media_item', 10, 2);
1198 add_action('media_upload_image', 'media_upload_image');
1199 add_action('media_upload_audio', 'media_upload_audio');
1200 add_action('media_upload_video', 'media_upload_video');
1201 add_action('media_upload_file', 'media_upload_file');
1202 add_action('admin_head_media_upload_type_form', 'media_admin_css');
1204 add_filter('media_upload_gallery', 'media_upload_gallery');
1205 add_action('admin_head_media_upload_gallery_form', 'media_admin_css');
1207 add_filter('media_upload_library', 'media_upload_library');
1208 add_action('admin_head_media_upload_library_form', 'media_admin_css');