get_attachment_fields_to_edit

Retrieves the attachment fields to edit form fields.

Description

(array) get_attachment_fields_to_edit( (WP_Post) $post, (constant) $errors = null ); 

Returns (array)

Parameters (2)

0. $post (WP_Post)
The post.
1. $errors — Optional. (constant) => null
The errors.

Usage

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

Defined (1)

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

/wp-admin/includes/media.php  
  1. function get_attachment_fields_to_edit($post, $errors = null) { 
  2. if ( is_int($post) ) 
  3. $post = get_post($post); 
  4. if ( is_array($post) ) 
  5. $post = new WP_Post( (object) $post ); 
  6.  
  7. $image_url = wp_get_attachment_url($post->ID); 
  8.  
  9. $edit_post = sanitize_post($post, 'edit'); 
  10.  
  11. $form_fields = array( 
  12. 'post_title' => array( 
  13. 'label' => __('Title'),  
  14. 'value' => $edit_post->post_title 
  15. ),  
  16. 'image_alt' => array(),  
  17. 'post_excerpt' => array( 
  18. 'label' => __('Caption'),  
  19. 'input' => 'html',  
  20. 'html' => wp_caption_input_textarea($edit_post) 
  21. ),  
  22. 'post_content' => array( 
  23. 'label' => __('Description'),  
  24. 'value' => $edit_post->post_content,  
  25. 'input' => 'textarea' 
  26. ),  
  27. 'url' => array( 
  28. 'label' => __('Link URL'),  
  29. 'input' => 'html',  
  30. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),  
  31. 'helps' => __('Enter a link URL or click above for presets.') 
  32. ),  
  33. 'menu_order' => array( 
  34. 'label' => __('Order'),  
  35. 'value' => $edit_post->menu_order 
  36. ),  
  37. 'image_url' => array( 
  38. 'label' => __('File URL'),  
  39. 'input' => 'html',  
  40. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",  
  41. 'value' => wp_get_attachment_url($post->ID),  
  42. 'helps' => __('Location of the uploaded file.') 
  43. ); 
  44.  
  45. foreach ( get_attachment_taxonomies($post) as $taxonomy ) { 
  46. $t = (array) get_taxonomy($taxonomy); 
  47. if ( ! $t['public'] || ! $t['show_ui'] ) 
  48. continue; 
  49. if ( empty($t['label']) ) 
  50. $t['label'] = $taxonomy; 
  51. if ( empty($t['args']) ) 
  52. $t['args'] = array(); 
  53.  
  54. $terms = get_object_term_cache($post->ID, $taxonomy); 
  55. if ( false === $terms ) 
  56. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); 
  57.  
  58. $values = array(); 
  59.  
  60. foreach ( $terms as $term ) 
  61. $values[] = $term->slug; 
  62. $t['value'] = join(', ', $values); 
  63.  
  64. $form_fields[$taxonomy] = $t; 
  65.  
  66. // Merge default fields with their errors, so any key passed with theerror(e.g. error, 'helps', 'value') will replace the default 
  67. // The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing ) 
  68. $form_fields = array_merge_recursive($form_fields, (array) $errors); 
  69.  
  70. // This was formerly in image_attachment_fields_to_edit(). 
  71. if ( substr($post->post_mime_type, 0, 5) == 'image' ) { 
  72. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true); 
  73. if ( empty($alt) ) 
  74. $alt = ''; 
  75.  
  76. $form_fields['post_title']['required'] = true; 
  77.  
  78. $form_fields['image_alt'] = array( 
  79. 'value' => $alt,  
  80. 'label' => __('Alternative Text'),  
  81. 'helps' => __('Alt text for the image, e.g. “The Mona Lisa”') 
  82. ); 
  83.  
  84. $form_fields['align'] = array( 
  85. 'label' => __('Alignment'),  
  86. 'input' => 'html',  
  87. 'html' => image_align_input_fields($post, get_option('image_default_align')),  
  88. ); 
  89.  
  90. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') ); 
  91.  
  92. } else { 
  93. unset( $form_fields['image_alt'] ); 
  94.  
  95. /** 
  96. * Filters the attachment fields to edit. 
  97. * @since 2.5.0 
  98. * @param array $form_fields An array of attachment form fields. 
  99. * @param WP_Post $post The WP_Post attachment object. 
  100. */ 
  101. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post ); 
  102.  
  103. return $form_fields;