Add Image Sizes to Media UI
<?php
/**
* Add image sizes to media UI
*
* Adds custom image sizes to the "Insert Media" user interface
* and adds custom class to the `<img>` tag.
*
* @since 1.0.0
* @access public
* @param array $sizes Gets the array of image size names.
* @global array $_wp_additional_image_sizes Gets the array of custom image size names.
* @return array $sizes Returns an array of image size names.
*/
function ccd_insert_custom_image_sizes( $sizes ) {
// Access global variables.
global $_wp_additional_image_sizes;
// Return default sizes if no custom sizes.
if ( empty( $_wp_additional_image_sizes ) ) {
return $sizes;
}
// Capitalize custom image size names and remove hyphens.
foreach ( $_wp_additional_image_sizes as $id => $data ) {
if ( ! isset( $sizes[$id] ) ) {
$sizes[$id] = ucwords( str_replace( [ '-', '_' ], ' ', $id ) );
}
}
// Return the modified array of sizes.
return $sizes;
}
// Add the operative filter.
add_filter( 'image_size_names_choose', 'ccd_insert_custom_image_sizes' );
WordPress Snippet
Adds custom image sizes to the "Insert Media" user interface and adds custom class to the <img>
tag. Also capitalizes and removes hyphens and underscores from custom image size name strings.