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/

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度