Created
June 16, 2023 21:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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