douxun1407 2017-10-16 07:22
浏览 49
已采纳

如何限制wp生成附件元数据()到某些中间图像大小

In Wordpress, we have added our own intermediate image sizes to the standard Wordpress sizes using add_image_size().

Using our own admin interface to upload images, we then use wp_generate_attachment_metadata() to create all thumbnails and save them to a predefined folder on the server.

However, we would like to restrict wp_generate_attachment_metadata() to generate our custom defined image sizes ONLY and ignore the Wordpress standard sizes.

Is this possible?

Thanks in advance for any help!

  • 写回答

1条回答 默认 最新

  • dongshi4589 2017-10-17 15:10
    关注

    It looks like there is a filter for the $sizes array generated when using wp_generate_attachement_metadata() called intermediate_image_sizes_advanced.

    I found this snippet that should do the trick if you put this in a functions file of some sort (normally it would probably go in your functions.php file):

    /**
     * Snippet Name: Disable auto creating of image sizes
     * Snippet URL: http://www.wpcustoms.net/snippets/disable-auto-creating-image-sizes/
     */
     function wpc_unset_imagesizes($sizes){
        unset( $sizes['thumbnail']);
        unset( $sizes['medium']);
        unset( $sizes['medium_large']);
        unset( $sizes['large']);
    }
    add_filter('intermediate_image_sizes_advanced', 'wpc_unset_imagesizes' );
    

    The above snippet should remove all of the default Wordpress image sizes from the $sizes array used in wp_generate_attachment_metadata() and only generate any custom image sizes you have added.

    NOTE: I have not tested this code, but it looks straight forward to me. If you want the filter to only effect your "own admin interface" you may not want to put the snippet in a separate functions file that only gets called in your admin interface.

    See the developer reference for the function and the hook below: https://developer.wordpress.org/reference/functions/wp_generate_attachment_metadata/ https://developer.wordpress.org/reference/hooks/intermediate_image_sizes_advanced/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM