Skip to content

Instantly share code, notes, and snippets.

@suiramus
Last active February 6, 2024 12:10
Show Gist options
  • Save suiramus/f6f1b712b0cbf68a2f3862fa82a1f336 to your computer and use it in GitHub Desktop.
Save suiramus/f6f1b712b0cbf68a2f3862fa82a1f336 to your computer and use it in GitHub Desktop.
Wordpress Image sizes
// Must be in 'after_setup_theme'
// add_action( 'after_setup_theme', 'image_size_function' );
// Post thumbnail and Images size
add_theme_support( 'post-thumbnails' );
// set_post_thumbnail_size( 500, 1000 );
add_image_size( 'gallery-thumb', 400, 800, false ); // Imagine folosita in galerie ca thumb
add_image_size( 'gallery-big', 1200, 1800, false ); // Imagine folosita in galerie ca thumb
// Display new image sizes in post
function wpb_custom_image_sizes( $size_names ) {
$new_sizes = array(
'gallery-thumb' => 'Gallery Thumbnail 400x800px',
'gallery-big' => 'Gallery Big Image 1200x1800px',
);
return array_merge( $size_names, $new_sizes );
}
add_filter( 'image_size_names_choose', 'wpb_custom_image_sizes' );
// ===================================================
// Function to get all the image sizes
function display_wp_image_sizes() {
global $_wp_additional_image_sizes;
$sizes = array();
foreach ( get_intermediate_image_sizes() as $_size ) {
if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
$sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" );
$sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" );
$sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop'],
);
}
}
return $sizes;
}
$get_thumbnails = display_wp_image_sizes();
// Print out all the image sizes
echo '<pre>';
print_r($get_thumbnails);
echo '</pre>';
/* ============================================== */
// Disabling the creation of additional sizes through the filter
// https://wp-kama.com/2003/disabling-wp-images-copies
add_filter( 'intermediate_image_sizes', 'delete_intermediate_image_sizes' );
function delete_intermediate_image_sizes( $sizes ){
// Sizes to be removed
return array_diff( $sizes, [
'medium_large',
'large',
'1536x1536',
'2048x2048',
] );
}
/* ============================================== */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment