Skip to content

Instantly share code, notes, and snippets.

@brandonjp
Created June 16, 2023 21:23
Show Gist options
  • Save brandonjp/d4a3b77280c38766d73fb76d7f9ef1cf to your computer and use it in GitHub Desktop.
Save brandonjp/d4a3b77280c38766d73fb76d7f9ef1cf to your computer and use it in GitHub Desktop.
For WordPress, Add a Metabox to List Attachments in the editor. Works on posts, pages & custom post types. - https://snipsnip.pro/s/724
// Add a Metabox to List Attachments in the editor - https://snipsnip.pro/s/724
if (!class_exists('AddPostAttachmentsMetaBox')) {
class AddPostAttachmentsMetaBox {
public function __construct() {
add_action('add_meta_boxes', array($this, 'add_metabox_of_post_attachments'));
}
public function add_metabox_of_post_attachments() {
$post_types = get_post_types(array('public' => true), 'names');
foreach ($post_types as $post_type) {
add_meta_box('att_thumb_display', 'Attachments', array($this, 'generate_list_of_post_attachments'), $post_type);
}
}
public function generate_list_of_post_attachments($post) {
$args = array(
'post_type' => 'attachment',
'order' => 'ASC',
'numberposts' => -1,
'post_parent' => $post->ID
);
echo '<ul>';
foreach (get_posts($args) as $key => $image) {
$imgSRC = esc_url(wp_get_attachment_url($image->ID));
$post_mime_type = esc_html($image->post_mime_type);
$edit_link = esc_url(get_edit_post_link($image->ID));
echo '<li style="display: flex;align-items: center;">' . $key . ': ' . $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="' . $edit_link . '" target="_blank" style="margin-left:1rem;">EDIT</a></li>';
}
echo '</ul>';
}
}
new AddPostAttachmentsMetaBox();
}
@brandonjp
Copy link
Author

For WordPress, Add a Metabox to List Attachments in the editor. Works on posts, pages & custom post types.
Add the code to your functions.php file or use Code Snippets Pro - Read more at: https://snipsnip.pro/s/724

CleanShot 2023-06-16 at 16 12 09@2x

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