Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active September 5, 2024 23:45
Show Gist options
  • Save atomjoy/a3649c97aa8d7bf97e3db5f99e118519 to your computer and use it in GitHub Desktop.
Save atomjoy/a3649c97aa8d7bf97e3db5f99e118519 to your computer and use it in GitHub Desktop.
How to enable post images and remove default generated image sizes in WordPress.
<?php
// Post formats
add_theme_support('post-formats', ['aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat']);
// Enable post thumbnails
add_theme_support('post-thumbnails');
// Update default thumbnails size (post-thumbnail, medium_large)
set_post_thumbnail_size(1280, 720, true);
add_image_size('medium_large', 768, 480, true);
// Add thumbnail custom sizes
add_image_size('post-thumbnail-large', 1280, 720, true);
add_image_size('post-thumbnail-medium', 640, 360, true);
add_image_size('post-thumbnail-small', 360, 200, true);
// Remove thumbnails
function remove_extra_image_sizes() {
foreach (get_intermediate_image_sizes() as $size) {
if (!in_array($size, array('medium_large', 'post-thumbnail', 'post-thumbnail-large', 'post-thumbnail-medium', 'post-thumbnail-small'))) {
remove_image_size( $size );
}
}
// Remove default thumbnails
update_option('thumbnail_size_h', 0);
update_option('thumbnail_size_w', 0);
update_option('medium_size_h', 0);
update_option('medium_size_w', 0);
update_option('medium_large_size_w', 0);
update_option('medium_large_size_h', 0);
update_option('large_size_h', 0);
update_option('large_size_w', 0);
}
// Add and next regenerate images with wp plugin
add_action('init', 'remove_extra_image_sizes');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment