开发者

How to auto generate custom image sizes

开发者 https://www.devze.com 2023-01-24 10:05 出处:网络
I got strange behaviour when I added filter for attachment_fields_to_save, when I save the images, it does not want to save image metadata such as title, description and caption.

I got strange behaviour when I added filter for attachment_fields_to_save, when I save the images, it does not want to save image metadata such as title, description and caption.

Why I need to add this filter? Because I have lots of custom sizes using this code:

add_image_size("imagesize-940x360", 940, 360, true);

And the image may not correctly put in good place, so the user need to use Wordpress awesome tools to edit the image like cropping and scaling.

For some silly reason (or perhaps this is bug), the Wordpress does not generate image for custom image sizes.

In order to achieve generation for custom sizes, I need to add filter when user press save button in the Wordpress image editor. Here is the piece code that I been using:

add_filter("attachment_fields_to_save", "rl_regenerate_image", 99, 2);
function rl_regenerate_image($post, $attachment)
{
    $id = $post['ID'];

    $fullsizepath = get_attached_file($id);
    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $fullsizepath));

    return true;
}

T开发者_StackOverflowhe code above generates the custom sizes when user edit the image correctly but sadly it does not save all updated metadata images such as title, caption and description.

Do you guys know how to solve this problem? So what I would like to achieve is how to generate "edited" images for custom sizes and save the metadata correctly.

Thanks in advance!


In case this hasn't been solved yet, or for any Googler's out there.. Here's a plugin called Post Thumbnail Editor that lets you edit individual custom image sizes. Works well.

http://wordpress.org/extend/plugins/post-thumbnail-editor/

0

精彩评论

暂无评论...
验证码 换一张
取 消