get_compat_media_markup

The WordPress Core get compat media markup function.

Description

(array) get_compat_media_markup( (int) $attachment_id, (null) $args = null ); 

Returns (array)

Parameters (2)

0. $attachment_id (int)
The attachment id.
1. $args — Optional. (null) => null
The args.

Usage

  1. if ( !function_exists( 'get_compat_media_markup' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/media.php'; 
  3.  
  4. // The attachment id. 
  5. $attachment_id = -1; 
  6.  
  7. // The args. 
  8. $args = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_compat_media_markup($attachment_id, $args); 
  12.  

Defined (1)

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

/wp-admin/includes/media.php  
  1. function get_compat_media_markup( $attachment_id, $args = null ) { 
  2. $post = get_post( $attachment_id ); 
  3.  
  4. $default_args = array( 
  5. 'errors' => null,  
  6. 'in_modal' => false,  
  7. ); 
  8.  
  9. $user_can_edit = current_user_can( 'edit_post', $attachment_id ); 
  10.  
  11. $args = wp_parse_args( $args, $default_args ); 
  12.  
  13. /** This filter is documented in wp-admin/includes/media.php */ 
  14. $args = apply_filters( 'get_media_item_args', $args ); 
  15.  
  16. $form_fields = array(); 
  17.  
  18. if ( $args['in_modal'] ) { 
  19. foreach ( get_attachment_taxonomies($post) as $taxonomy ) { 
  20. $t = (array) get_taxonomy($taxonomy); 
  21. if ( ! $t['public'] || ! $t['show_ui'] ) 
  22. continue; 
  23. if ( empty($t['label']) ) 
  24. $t['label'] = $taxonomy; 
  25. if ( empty($t['args']) ) 
  26. $t['args'] = array(); 
  27.  
  28. $terms = get_object_term_cache($post->ID, $taxonomy); 
  29. if ( false === $terms ) 
  30. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); 
  31.  
  32. $values = array(); 
  33.  
  34. foreach ( $terms as $term ) 
  35. $values[] = $term->slug; 
  36. $t['value'] = join(', ', $values); 
  37. $t['taxonomy'] = true; 
  38.  
  39. $form_fields[$taxonomy] = $t; 
  40.  
  41. // Merge default fields with their errors, so any key passed with theerror(e.g. error, 'helps', 'value') will replace the default 
  42. // The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing ) 
  43. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] ); 
  44.  
  45. /** This filter is documented in wp-admin/includes/media.php */ 
  46. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post ); 
  47.  
  48. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],  
  49. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],  
  50. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] ); 
  51.  
  52. /** This filter is documented in wp-admin/includes/media.php */ 
  53. $media_meta = apply_filters( 'media_meta', '', $post ); 
  54.  
  55. $defaults = array( 
  56. 'input' => 'text',  
  57. 'required' => false,  
  58. 'value' => '',  
  59. 'extra_rows' => array(),  
  60. 'show_in_edit' => true,  
  61. 'show_in_modal' => true,  
  62. ); 
  63.  
  64. $hidden_fields = array(); 
  65.  
  66. $item = ''; 
  67. foreach ( $form_fields as $id => $field ) { 
  68. if ( $id[0] == '_' ) 
  69. continue; 
  70.  
  71. $name = "attachments[$attachment_id][$id]"; 
  72. $id_attr = "attachments-$attachment_id-$id"; 
  73.  
  74. if ( !empty( $field['tr'] ) ) { 
  75. $item .= $field['tr']; 
  76. continue; 
  77.  
  78. $field = array_merge( $defaults, $field ); 
  79.  
  80. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) ) 
  81. continue; 
  82.  
  83. if ( $field['input'] == 'hidden' ) { 
  84. $hidden_fields[$name] = $field['value']; 
  85. continue; 
  86.  
  87. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : ''; 
  88. $required = $field['required'] ? '<span class="required">*</span>' : ''; 
  89. $required_attr = $field['required'] ? ' required' : ''; 
  90. $aria_required = $field['required'] ? " aria-required='true'" : ''; 
  91. $class = 'compat-field-' . $id; 
  92. $class .= $field['required'] ? ' form-required' : ''; 
  93.  
  94. $item .= "\t\t<tr class='$class'>"; 
  95. $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>"; 
  96. $item .= "</th>\n\t\t\t<td class='field'>"; 
  97.  
  98. if ( !empty( $field[ $field['input'] ] ) ) 
  99. $item .= $field[ $field['input'] ]; 
  100. elseif ( $field['input'] == 'textarea' ) { 
  101. if ( 'post_content' == $id && user_can_richedit() ) { 
  102. // sanitize_post() skips the post_content when user_can_richedit. 
  103. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES ); 
  104. $item .= "<textarea id='$id_attr' name='$name'{$required_attr}{$aria_required}>" . $field['value'] . '</textarea>'; 
  105. } else { 
  106. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr}{$aria_required} />"; 
  107. if ( !empty( $field['helps'] ) ) 
  108. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>'; 
  109. $item .= "</td>\n\t\t</tr>\n"; 
  110.  
  111. $extra_rows = array(); 
  112.  
  113. if ( !empty( $field['errors'] ) ) 
  114. foreach ( array_unique( (array) $field['errors'] ) as $error ) 
  115. $extra_rows[error][] = $error; 
  116.  
  117. if ( !empty( $field['extra_rows'] ) ) 
  118. foreach ( $field['extra_rows'] as $class => $rows ) 
  119. foreach ( (array) $rows as $html ) 
  120. $extra_rows[$class][] = $html; 
  121.  
  122. foreach ( $extra_rows as $class => $rows ) 
  123. foreach ( $rows as $html ) 
  124. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n"; 
  125.  
  126. if ( !empty( $form_fields['_final'] ) ) 
  127. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n"; 
  128.  
  129. if ( $item ) { 
  130. $item = '<p class="media-types media-types-required-info">' . 
  131. sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . '</p> 
  132. <table class="compat-attachment-fields">' . $item . '</table>'; 
  133.  
  134. foreach ( $hidden_fields as $hidden_field => $value ) { 
  135. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n"; 
  136.  
  137. if ( $item ) 
  138. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item; 
  139.  
  140. return array( 
  141. 'item' => $item,  
  142. 'meta' => $media_meta,  
  143. );