To add a custom field to attachments, place the following code in your functions.php.
add_filter('attachment_fields_to_edit', 'edit_media_custom_field', 11, 2 );
add_filter('attachment_fields_to_save', 'save_media_custom_field', 11, 2 );
function edit_media_custom_field( $form_fields, $post ) {
$form_fields['custom_field'] = array( 'label' => 'Custom Field', 'input' => 'text', 'value' => get_post_meta( $post->ID, '_custom_field', true ) );
return $form_fields;
}
function save_media_custom_field( $post, $attachment ) {
update_post_meta( $post['ID'], '_custom_field', $attachment['custom_field'] );
return $post;
}
To output that data you can use the following in the loop:
get_post_meta( get_the_ID(), '_custom_field', true ) );
Related Posts:
- Remove Dimension from wp_get_attachment_image
- Add multiple images to a page sidebar
- Display info from custom fields in all images’ HTML
- copy attachments to another post type and change attachment url
- How to delete attachments associated with custom field type when post property changes? [closed]
- Adding thumbnails for non-image attachments
- explode array within shortcode
- How can I append custom data to images in the editor?
- Change wp_get_attachment_image attributes (src and srcset) on specific custom field
- How do I make a custom field choose an image?
- How can I remove fields in the attachment editor?
- Extend WP Customizer to make multiple image selection possible
- Saving Custom Field in Attachment Window in WordPress 3.5
- How to set a default meta value for custom field
- Display thumbnail from custom field
- How to save custom fields for attachments
- Stopping WordPress from Auto Generating Image Files for Sizes
- automatically set “Featured Image” the same as the og:i that is set in a custom field
- limit amount of photos uploaded per cpt post
- ajax delete value from custom field array
- Save attachment custom fields on front end
- Metabox with multiple fields added by user and upload box
- Add WYSIWYG to Image Description field
- Insert attachment ID in custom field from media uploader
- Get Meta from Custom Field of Image URL
- Add url from Custom Field as ‘Featured Image’. Code not working
- How can I store a file in the database in the same way WordPress and ACF do?
- How to display multiple images in custom field
- How to add image attachment to post from a single meta key (Woocommerce)
- Getting the ID of any image for use in functions.php
- Exclude images uploaded via meta boxes from WordPress gallery
- Validate data on attachment_fields_to_save
- How do i save and retrieve custom attachment meta?
- Custom field not updating when value is empty
- How to add the image URL from an external RSS feed and insert into a custom field?
- how to upload a image from frontend with wp_insert_post and also update_post_meta?
- Add new image block, set default class name and update it using Javascript
- Better way to save image in custom field
- How do I add an image upload, custom field to a WooCommerce product?
- For homepage images (for small business website), is it better to use custom fields or post_thumbnail?
- Select multiple images from custom field
- Displaying page image in the footer automatically
- Get image url using image id
- Link FROM attachment to full post and get custom fields values on attachment page?
- Show image if author meta (profile fields) exists outside loop
- Attach images to posts using custom fields (just paths to images already uploaded)
- Convert attachment ID into url?
- Custom field as featured image caption
- Show custom field on attachment page?
- Display a different image for each page with editor
- Save attachment ID instead of URL in custom field
- Image upload and path to custom field
- How to get custom image field of specific post id
- Selectively hiding or allowing thumbnails of featured images on front page
- wp_get_attachment_image_src() with advanced custom fields returning empty
- Unable to select image with custom field
- Users uploaded image and matching an id or taxonomy
- Shortcode for Custom Field of Media Attachment (to use with Featured Images)
- Custom field in media library not saving, selected() function not adding “selected” to select list input type
- Adding Facebook’s image and meta description retrieval capabilities to a WordPress post
- Show image depending on the number in the custom fields
- Use a different catalog image than the featured image
- How to add custom fields to images for image source text and URL
- Get first URL from custom field, download and set as featured image on post publish
- Change path to child of page when uploading image via custom field
- Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
- Analyze uploaded images to get colour values
- Upload image to wordpress
- Undefined Variable (Displaying image via custom field)
- Multiple image uploader under editor?
- Get single image from custom fields made with WCK custom fields creator
- Add image custom attribute [closed]
- How can I attach files without using a plugin?
- How to mark a image attachment as background image?
- Crop custom image size vs actual size
- How to display custom fields in hestia theme
- How to update only certain custom fields in a the cache of a page?
- Woocommerce products search with custom fields
- Custom meta fields and meta keys
- Making custom meta box required (with error message if not filled in) on Gutenberg
- HTML for adding a meta box (basic text field) to page editor?
- Convert author metadata to a custom field
- Adding Custom Metadata to my Archive/Posts page
- Is the use of many custom field not good for server?
- Move the post title to another field
- How to save multiple values with same meta_key, each value linked to another tag id
- Move the metaboxes to the very top of post editor
- ACF multi taxonomy on filterable gallery
- Arrays in custom fields or is there a better way?
- How to improve my non-unique metadata MySQL entries?
- Use value from ACF to populate other fields
- Dynamically populate query source in Elementor post widget
- Custom date column in user table not sorting correcting
- Set class if a meta value is set within post archive
- Add link to wordpress field data
- Adding text box with add_meta_box
- pass custom value from single.php to another file via get
- Custom image size not displaying with wp_get_attachment_image()
- How to update an image attachment’s alt text from a custom field when saving a post?
- Changing form action based on selected value