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
- WP doesn’t show Array Custom Fields?
- How to change _wp_attachment_metadata specific value in SQL?
- How do I read the value of a custom field of the page?
- Displaying additional User Contact Information
- get_post_meta in sitewide tags plugin fails to return value for custom fields
- Making custom field’s ‘Name’ available in the dropdown list by default in a theme?
- WooCommerce conditional required checkout fields [closed]
- Using more than one meta_key in pre_posts_query
- Display attachment resolution size
- Creating author profiles with extra fields and exporting that data?
- Removing link ” from ” on meta_value in custom feilds
- How to add a custom field in the advanced menu properties?
- Meta query with multiple custom fields for archives page ordering problem
- filter custom field values $min $max
- How to use pre_get_posts to alter posts_per_page of category pages, where ‘posts_per_page’ will be dynamic
- How to validation for sanitize_URL?
- How can I output the custom fields wrapping with HTML
- Use full size images in a specific gallery
- Woocommerce products search with custom fields
- Convert author metadata to a custom field
- select user with all meta field and field value. I am use Below Query for this ,So any of know another way to fast query instead of below query?
- Show comon custom field results?
- Display Child Page with custom fields within Parent Page
- Incomplete attachment page
- list of attachments: wp_get_attachment_link works, wp_get_attachment_image does not