admin管理员组文章数量:1279148
The project I'm working on, I couldn't find an easy way to have WordPress create the thumbnails sizes I needed without tons of waste, nor to simultaneously generate WebP versions, so I disabled the thumbnails, I upload a full-size jpg or png, and then I manually generate my WebP and other sizes and copy them to the uploads. A custom PHP function handles building responsive <picture>
.
However, I'd like to avoid the media library loading up the full-size image as the thumbnail all the time, as it's a waste of bandwidth. I'd like to point to a small WebP or JPG version that is not in the library but in the uploads folder and have it load that image instead. The file name is almost the same, just needs to be appended with _width
(width being a number) which I could do with strtr
or something.
Is there a way to access the Media Library File column with a function and a hook/filter so I can change the src path to the thumbnail image? I'd also need to avoid PDF
, SVG
and TXT
file uploads and just target JPG
or PNG
files. If it's not possible, I'll have to generate a thumbnail with a custom size.
The project I'm working on, I couldn't find an easy way to have WordPress create the thumbnails sizes I needed without tons of waste, nor to simultaneously generate WebP versions, so I disabled the thumbnails, I upload a full-size jpg or png, and then I manually generate my WebP and other sizes and copy them to the uploads. A custom PHP function handles building responsive <picture>
.
However, I'd like to avoid the media library loading up the full-size image as the thumbnail all the time, as it's a waste of bandwidth. I'd like to point to a small WebP or JPG version that is not in the library but in the uploads folder and have it load that image instead. The file name is almost the same, just needs to be appended with _width
(width being a number) which I could do with strtr
or something.
Is there a way to access the Media Library File column with a function and a hook/filter so I can change the src path to the thumbnail image? I'd also need to avoid PDF
, SVG
and TXT
file uploads and just target JPG
or PNG
files. If it's not possible, I'll have to generate a thumbnail with a custom size.
1 Answer
Reset to default 1I managed to solve this after being pointed in the right direction. In my case, I filtered wp_get_attachment_image_attributes
to change all the paths in the admin for attachments at once.
In my case, I am using descriptive suffixes on all my image files (i.e. _featured
etc) so modifying them was easy using strtr
which I also used to change the extension from jpg to webp.
Now all attachment image previews load in the files I want in the correct format at the desired size, and it works for Media Library, featured images, and custom fields.
add_filter( 'wp_get_attachment_image_attributes', 'my_attachment_filter', 10, 3 );
function my_attachment_filter($attr, $attachment, $size){
if (is_admin()){
if (array_key_exists( 'src' , $attr)){
$old_src = $attr['src'];
$new_src = strtr($old_src, array('_featured' => '_featured_eighth', '_portrait' => '_portrait_204', '_og.' => '_og_320.', '.jpg' => '.webp'));
$attr['src'] = $new_src;
}
}
return $attr;
}
本文标签: functionsPossible to hook into Media Library preview File column and use a custom image
版权声明:本文标题:functions - Possible to hook into Media Library preview File column and use a custom image? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741257033a2366877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_enqueue_media
might be the way, but not sure how to go about it. I'll add a screenshot. – liquidRock Commented Oct 8, 2021 at 0:15image_size
s I wanted based on a file prefix, but no one responded. As it is, I already have 2,000 images do to responsive and the site isn't live, and I couldn't keep waiting without a reply, so this is the route I took. Everything works great and I use a plugin to organize uploads into directories. Just need to modify the admin thumbnail src somehow. – liquidRock Commented Oct 8, 2021 at 0:22