Skip to content

Instantly share code, notes, and snippets.

@brandonjp
Last active January 18, 2023 20:29
Show Gist options
  • Save brandonjp/c87a1f57be5578dc2f2300f4bab38d25 to your computer and use it in GitHub Desktop.
Save brandonjp/c87a1f57be5578dc2f2300f4bab38d25 to your computer and use it in GitHub Desktop.
WP Post Edit: Add Metabox to List Attachments - WP / Code Snippets Pro
<? // <- remove this line if you copy/paste into Code Snippets Pro
namespace cpZgLjBJ;
function add_metabox_of_post_attachments()
{
add_meta_box('att_thumb_display', 'Attachments', '\cpZgLjBJ\generate_list_of_post_attachments', 'post'); // Change your post type here
}
function generate_list_of_post_attachments($post)
{
// get_posts: https://developer.wordpress.org/reference/functions/get_posts/
$args = array(
'post_type' => 'attachment',
'order' => 'ASC',
// 'post_mime_type' => 'image', // ignore this to get all, else use 'image', 'video', 'audio'
'numberposts' => -1,
'post_parent' => $post->ID
);
echo '<ul>';
foreach (get_posts($args) as $key=>$image) {
$imgSRC = wp_get_attachment_url($image->ID);
echo '<li style="display: flex;align-items: center;">'.$key.': '.$image->post_mime_type.'<a href="' . $imgSRC . '" target="_blank" style="margin-left:1rem;display: flex;align-items: center;"><img style="max-width:150px;margin-right:1rem;" src="' . $imgSRC . '">' . $imgSRC . ' ↗</a> <a href="post.php?post='.$image->ID.'&action=edit" target="_blank" style="margin-left:1rem;">EDIT</a></li>';
}
echo '</ul>';
}
add_action('add_meta_boxes', '\cpZgLjBJ\add_metabox_of_post_attachments');
@brandonjp
Copy link
Author

PHP snippet for Code Snippets Pro
Adds a metabox to the post edit screen to show a list of attached media files.

CleanShot 2023-01-18 at 14 23 26@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment