Skip to content

Instantly share code, notes, and snippets.

@wycks
Last active December 16, 2020 01:48
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save wycks/4949242 to your computer and use it in GitHub Desktop.
Save wycks/4949242 to your computer and use it in GitHub Desktop.
Remove WordPress full size images from being inserted into a post + option to and add max size to to prevent users from inserting massive images.
<?php
/**
*
* This removes the ability to add the FULL image size into a post, it does not alter or delete the image
* Add whataever extra image sizes to the insert dropdown in WordPress you create via add_image_size
*
* For now we have to do it this way to make the labels translatable, see trac ref below.
*
* If your theme has $content_width GLOBAL make sure and remove it
*/
//example to add post-size instead of using $content_width and a max-size
add_image_size( 'post-size', 600, 9999);
add_image_size( 'max-size', 1600, 900, true);
// over-ride image_size_names_choose
function add_image_insert_override($size_names){
global $_wp_additional_image_sizes;
//default array with hardcoded values for add_image_size
$size_names = array(
'thumbnail' => __('Thumbnail'),
'medium' => __('Medium'),
'large' => __('Large'),
'post-size' => __('Post Size'),
'max-size' => __('Max Size')
//'full' => __('Full Size') bye bye
);
return $size_names;
};
add_filter('image_size_names_choose', 'add_image_insert_override' );
// For referance
//http://core.trac.wordpress.org/ticket/20663, http://core.trac.wordpress.org/ticket/19990
?>
@andrienko
Copy link

Thank you! You have saved my day.

I needed to limit sizes of images to forbid inserting too big ones in posts, because apple devices dont load images that are too big on webpages.

I have also added:

update_option('medium_size_h', 1024);
update_option('large_size_h', 1600);

@Shakil1081
Copy link

function sgr_filter_image_sizes( $sizes) {

unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);

return $sizes;

}
add_filter('intermediate_image_sizes_advanced', 'sgr_filter_image_sizes');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment