Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dompl/69ab6029b247fa86c3e298d6f1359229 to your computer and use it in GitHub Desktop.
Save dompl/69ab6029b247fa86c3e298d6f1359229 to your computer and use it in GitHub Desktop.
Add Attachment ID to WordPress Media Library Details Panel
<?php
/**
* Add Attachment ID to WordPress Media Library Details Panel.
*
* This snippet adds a new, non-editable field to the "Attachment Details"
* panel in the WordPress Media Library, displaying the media file's unique ID.
*
* HOW TO USE:
* 1. The BEST way: Use a code snippets plugin like WPCode.
* 2. The ADVANCED way: Add it to a custom functionality plugin.
* 3. The RISKY way (not recommended): Add it to your active theme's functions.php file.
*/
/**
* Adds the Attachment ID field to the attachment edit form.
*
* @param array $form_fields An array of attachment form fields.
* @param WP_Post $post The attachment WP_Post object.
* @return array The modified array of form fields.
*/
function add_attachment_id_to_media_details( $form_fields, $post ) {
// Create the new field
$form_fields['attachment_id'] = array(
'label' => 'Attachment ID',
'input' => 'html',
'html' => '<span>' . $post->ID . '</span>',
'helps' => 'The unique ID for this media file.'
);
return $form_fields;
}
// Hook the function into the 'attachment_fields_to_edit' filter.
add_filter( 'attachment_fields_to_edit', 'add_attachment_id_to_media_details', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment