Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve the extensions of images uploaded in WordPress media gallery
/**
* Retrieve all the medias uploaded in the media library
* with wp_get_attachment_url()
*
* @since 2.1.0
*
* @param (int) (Optional) Attachment post ID. Defaults to global $post.
*
* @link https://developer.wordpress.org/reference/functions/wp_get_attachment_url
*/
function co_retrieve_images() {
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => '-1',
);
$query_images = new WP_Query( $query_images_args );
foreach ( $query_images->posts as $image_info ) {
$co_medias_url_list[] = wp_get_attachment_url( $image_info->ID );
}
return $co_medias_url_list;
}
/**
* Retrieve the extensions of medias uploaded in the media gallery
* using wp_check_filetype()
*
* @since 2.0.4
*
* @param (string) (Required) File name or path.
*
* @link https://developer.wordpress.org/reference/functions/wp_check_filetype
*/
function co_images_extensions() {
$medias_url_list = co_retrieve_images();
$single_image_extension = '';
$images_extension_list = [];
foreach ( $medias_url_list as $single_image_url ) {
$single_image_extension = wp_check_filetype( $single_image_url );
$images_extension_list[] = strtoupper( $single_image_extension['ext'] );
}
return $images_extension_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.