How to retrieve the files type of WordPress media gallery files?

Last month, while I was working on a WordPress plugin, I needed to find and return in a readable format the extensions of the images uploaded to the media gallery (png, jpeg, etc…).

At the time, I thought about using the full paths of the images with their names and then extracting the extension with native PHP functions as substr() combined with explode(). I tried to implement the solution this way, but I realized that it was not the optimal method.

So I started searching the internet for a better solution, and it really wasn’t a pleasure. Among other things, I had trouble finding the right wording for my problem. Also, the solution approaches I found while browsing the forums, although some of them worked, were not all WordPress oriented. It must be said that when developing for WordPress, I always try to use the native features of the CMS, for more comfort.

Guys who overdo it on StackOverflow

A small search in the WordPress developer reference handbook then allowed me to discover a rather useful function that perfectly met my expectations.

It is called wp_check_filetype(), and was introduced in 2006 with version 2.0.4 of WordPress. Basically, the function takes as main parameter the name or path to the file (or the image in my case) and returns its type. Quite simple.

Here is a basic description of how I used it.

I first request a query to get the URL of the images uploaded with the function wp_get_attachment_url(). This step can be done in several other ways.

/**
 * 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;
}

I then use the table of paths to the images that I pass one by one as a parameter to the wp_check_filetype() function.

/** 
 * 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;
}

These examples are deliberately simplified and detailed to make the code more transparent. The complete source is available here.

Voila, now we have the extensions of our images that we can use as we please. For example, I encoded mine in JSON to generate graphs with FusionCharts.

Images extensions chart WPMA

Well, bye-bye ?. Have fun. And see you again soon.

Featured image by Shahadat Shemul on Unsplash.

Publié par segbedji

Hi ??. Je suis Justin. Je suis un développeur et contributeur actif au cœur de WordPress basé à Cotonou au Bénin. Depuis 2019, je participe activement au développement du CMS.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *