最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uploads - Unset image sizes before images are uploaded

programmeradmin3浏览0评论

I would like to unset image sizes before uploading images that are not meant to be featured images. Right now I have a clunky workflow where, whenever I upload images, I comment out the custom image sizes use for featured images, then bulk upload non-featured images. Then I uncomment those lines and then upload the featured images. I have a photosite with thousands of photos so making sure I don't have useless image sizes for non-featured images is important.

I would like to know if there is a way to add a checkbox (probably via a plugin that I create) to the upload new media page at Dashboard > Media > Add New, when checked, it will run a simple piece of code before the media is uploaded.

This is the code I would run if he box is checked.

function disable_featured_image_sizes($sizes)
{
  unset($sizes['my_small_square']);
  return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'disable_featured_image_sizes');

My problem is I don't know if it's possible to add such a checkbox to the Add New Media page and, if it was possible, how to get this value in my plugin.

I would like to unset image sizes before uploading images that are not meant to be featured images. Right now I have a clunky workflow where, whenever I upload images, I comment out the custom image sizes use for featured images, then bulk upload non-featured images. Then I uncomment those lines and then upload the featured images. I have a photosite with thousands of photos so making sure I don't have useless image sizes for non-featured images is important.

I would like to know if there is a way to add a checkbox (probably via a plugin that I create) to the upload new media page at Dashboard > Media > Add New, when checked, it will run a simple piece of code before the media is uploaded.

This is the code I would run if he box is checked.

function disable_featured_image_sizes($sizes)
{
  unset($sizes['my_small_square']);
  return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'disable_featured_image_sizes');

My problem is I don't know if it's possible to add such a checkbox to the Add New Media page and, if it was possible, how to get this value in my plugin.

Share Improve this question edited Jul 31, 2020 at 16:56 OctaviaLo asked Jul 31, 2020 at 13:32 OctaviaLoOctaviaLo 1398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You get $sizes as array, where size name is a value, not a key

Try this

    //remove sizes with init hook and remove_image_size
    add_action('init', function ($sizes) {
        remove_image_size('2048x2048');
        remove_image_size('1536x1536');
        return $sizes;
    });

    // or just return sizes you need
    add_filter('intermediate_image_sizes', function ($sizes) {
        return ['thumbnail','medium','large'];
    });
发布评论

评论列表(0)

  1. 暂无评论