get_media_item

Retrieve HTML form for modifying the image attachment.

Description

(string) get_media_item( (int) $attachment_id, (null) $args = null ); 

Returns (string)

HTML form for attachment.

Parameters (2)

0. $attachment_id (int)
Attachment ID for modification.
1. $args — Optional. (null) => null
Override defaults.

Usage

  1. if ( !function_exists( 'get_media_item' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/media.php'; 
  3.  
  4. // Attachment ID for modification. 
  5. $attachment_id = -1; 
  6.  
  7. // Optional. Override defaults. 
  8. $args = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_media_item($attachment_id, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/media.php  
  1. function get_media_item( $attachment_id, $args = null ) { 
  2. global $redir_tab; 
  3.  
  4. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) ) 
  5. $thumb_url = $thumb_url[0]; 
  6. else 
  7. $thumb_url = false; 
  8.  
  9. $post = get_post( $attachment_id ); 
  10. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0; 
  11.  
  12. $default_args = array( 
  13. 'errors' => null,  
  14. 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,  
  15. 'delete' => true,  
  16. 'toggle' => true,  
  17. 'show_title' => true 
  18. ); 
  19. $args = wp_parse_args( $args, $default_args ); 
  20.  
  21. /** 
  22. * Filters the arguments used to retrieve an image for the edit image form. 
  23. * @since 3.1.0 
  24. * @see get_media_item 
  25. * @param array $args An array of arguments. 
  26. */ 
  27.  
  28. $toggle_on = __( 'Show' ); 
  29. $toggle_off = __( 'Hide' ); 
  30.  
  31. $file = get_attached_file( $post->ID ); 
  32. $filename = esc_html( wp_basename( $file ) ); 
  33. $title = esc_attr( $post->post_title ); 
  34.  
  35. $post_mime_types = get_post_mime_types(); 
  36. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) ); 
  37. $type = reset( $keys ); 
  38. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />"; 
  39.  
  40. $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] ); 
  41.  
  42. if ( $r['toggle'] ) { 
  43. $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen'; 
  44. $toggle_links = " 
  45. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a> 
  46. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>"; 
  47. } else { 
  48. $class = ''; 
  49. $toggle_links = ''; 
  50.  
  51. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case 
  52. $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '…' ) . "</span></div>" : ''; 
  53.  
  54. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) ); 
  55. $order = ''; 
  56.  
  57. foreach ( $form_fields as $key => $val ) { 
  58. if ( 'menu_order' == $key ) { 
  59. if ( $gallery ) 
  60. $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>"; 
  61. else 
  62. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />"; 
  63.  
  64. unset( $form_fields['menu_order'] ); 
  65. break; 
  66.  
  67. $media_dims = ''; 
  68. $meta = wp_get_attachment_metadata( $post->ID ); 
  69. if ( isset( $meta['width'], $meta['height'] ) ) 
  70. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']} × {$meta['height']}</span> "; 
  71.  
  72. /** 
  73. * Filters the media metadata. 
  74. * @since 2.5.0 
  75. * @param string $media_dims The HTML markup containing the media dimensions. 
  76. * @param WP_Post $post The WP_Post attachment object. 
  77. */ 
  78. $media_dims = apply_filters( 'media_meta', $media_dims, $post ); 
  79.  
  80. $image_edit_button = ''; 
  81. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) { 
  82. $nonce = wp_create_nonce( "image_editor-$post->ID" ); 
  83. $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>"; 
  84.  
  85. $attachment_url = get_permalink( $attachment_id ); 
  86.  
  87. $item = " 
  88. $type_html 
  89. $toggle_links 
  90. $order 
  91. $display_title 
  92. <table class='slidetoggle describe $class'> 
  93. <thead class='media-item-info' id='media-head-$post->ID'> 
  94. <tr> 
  95. <td class='A1B1' id='thumbnail-head-$post->ID'> 
  96. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p> 
  97. <p>$image_edit_button</p> 
  98. </td> 
  99. <td> 
  100. <p><strong>" . __('File name:') . "</strong> $filename</p> 
  101. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p> 
  102. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( __( 'F j, Y' ), $post->post_date ). '</p>'; 
  103. if ( !empty( $media_dims ) ) 
  104. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n"; 
  105.  
  106. $item .= "</td></tr>\n"; 
  107.  
  108. $item .= " 
  109. </thead> 
  110. <tbody> 
  111. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>\n 
  112. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n 
  113. <tr><td colspan='2'><p class='media-types media-types-required-info'>" . sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . "</p></td></tr>\n"; 
  114.  
  115. $defaults = array( 
  116. 'input' => 'text',  
  117. 'required' => false,  
  118. 'value' => '',  
  119. 'extra_rows' => array(),  
  120. ); 
  121.  
  122. if ( $r['send'] ) { 
  123. $r['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false ); 
  124.  
  125. $delete = empty( $r['delete'] ) ? '' : $r['delete']; 
  126. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) { 
  127. if ( !EMPTY_TRASH_DAYS ) { 
  128. $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>'; 
  129. } elseif ( !MEDIA_TRASH ) { 
  130. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a> 
  131. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . 
  132. /** translators: %s: file name */ 
  133. '<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p> 
  134. <a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a> 
  135. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a> 
  136. </div>"; 
  137. } else { 
  138. $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a> 
  139. <a href='" . wp_nonce_url( "post.php?action=untrash&post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>"; 
  140. } else { 
  141. $delete = ''; 
  142.  
  143. $thumbnail = ''; 
  144. $calling_post_id = 0; 
  145. if ( isset( $_GET['post_id'] ) ) { 
  146. $calling_post_id = absint( $_GET['post_id'] ); 
  147. } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set 
  148. $calling_post_id = $post->post_parent; 
  149. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) ) 
  150. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) { 
  151.  
  152. $calling_post = get_post( $calling_post_id ); 
  153. $calling_post_type_object = get_post_type_object( $calling_post->post_type ); 
  154.  
  155. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" ); 
  156. $thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . "</a>"; 
  157.  
  158. if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) { 
  159. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" ); 
  160. $hidden_fields = array(); 
  161.  
  162. foreach ( $form_fields as $id => $field ) { 
  163. if ( $id[0] == '_' ) 
  164. continue; 
  165.  
  166. if ( !empty( $field['tr'] ) ) { 
  167. $item .= $field['tr']; 
  168. continue; 
  169.  
  170. $field = array_merge( $defaults, $field ); 
  171. $name = "attachments[$attachment_id][$id]"; 
  172.  
  173. if ( $field['input'] == 'hidden' ) { 
  174. $hidden_fields[$name] = $field['value']; 
  175. continue; 
  176.  
  177. $required = $field['required'] ? '<span class="required">*</span>' : ''; 
  178. $required_attr = $field['required'] ? ' required' : ''; 
  179. $aria_required = $field['required'] ? " aria-required='true'" : ''; 
  180. $class = $id; 
  181. $class .= $field['required'] ? ' form-required' : ''; 
  182.  
  183. $item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>"; 
  184. if ( !empty( $field[ $field['input'] ] ) ) 
  185. $item .= $field[ $field['input'] ]; 
  186. elseif ( $field['input'] == 'textarea' ) { 
  187. if ( 'post_content' == $id && user_can_richedit() ) { 
  188. // Sanitize_post() skips the post_content when user_can_richedit. 
  189. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES ); 
  190. // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit(). 
  191. $item .= "<textarea id='$name' name='$name'{$required_attr}{$aria_required}>" . $field['value'] . '</textarea>'; 
  192. } else { 
  193. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr}{$aria_required} />"; 
  194. if ( !empty( $field['helps'] ) ) 
  195. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>'; 
  196. $item .= "</td>\n\t\t</tr>\n"; 
  197.  
  198. $extra_rows = array(); 
  199.  
  200. if ( !empty( $field['errors'] ) ) 
  201. foreach ( array_unique( (array) $field['errors'] ) as $error ) 
  202. $extra_rows[error][] = $error; 
  203.  
  204. if ( !empty( $field['extra_rows'] ) ) 
  205. foreach ( $field['extra_rows'] as $class => $rows ) 
  206. foreach ( (array) $rows as $html ) 
  207. $extra_rows[$class][] = $html; 
  208.  
  209. foreach ( $extra_rows as $class => $rows ) 
  210. foreach ( $rows as $html ) 
  211. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n"; 
  212.  
  213. if ( !empty( $form_fields['_final'] ) ) 
  214. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n"; 
  215. $item .= "\t</tbody>\n"; 
  216. $item .= "\t</table>\n"; 
  217.  
  218. foreach ( $hidden_fields as $name => $value ) 
  219. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n"; 
  220.  
  221. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) { 
  222. $parent = (int) $_REQUEST['post_id']; 
  223. $parent_name = "attachments[$attachment_id][post_parent]"; 
  224. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n"; 
  225.  
  226. return $item;