WordPress 5.8 compatibility
-
Hi,
is this plugin still maintained? It sais compatible with 5.7, while 5.8 is out since a long time now.
-
Hi @nicohood,
Yes, it is still mantained, and it is compatible with 5.8. We’ll update the plugin as soon as we can.
Best,
Thanks for your fast reply.
I checked your code and tried to minimize it as a simple code snippet. While looking through the code, I noticed, that you are not using $saved_image after the image has been saved. The new image has new dimensions, so I’d expect the correct code would be:
// old //$saved_image = $image_editor->save($image_data['file']); // new $image_data = $image_editor->save($image_data['file']);I am no expert in that, maybe it does not matter at all what the function returns, but that looks more correct to me. Am I wrong?
Also I am wondering why you are returning the value, if we are using an action, not a filter? I am new to wordpress, but it looks like you should either remove the return value or use a filter instead.
Another suggestion:
Add webp to valid types, but also check if the mime type is supported by the editor:// Check if the image editor supports the image type if(!$image_edito->supports_mime_type($image_data['type'])) { return $image_data; }Here is my current working state, to just resize those images:
php // Hook the function to the upload handler // TODO use a filter instead? add_action('wp_handle_upload', 'resize_image_after_upload'); function resize_image_after_upload($image_data){ // Set to null to disable that width/height resizing $max_width = 1920; $max_height = 1920; // Check if there is a valid file if(empty($image_data['file']) || empty($image_data['type'])) { return $image_data; } // NOTE: We are not resizing any gifs, to avoid resizing animated gifs // (which I think is the most common gif nowadays) $valid_types = array('image/png','image/jpeg','image/jpg', 'image/webp'); if(!in_array($image_data['type'], $valid_types)) { return $image_data; } // Get image image_editor // https://developer.ww.wp.xz.cn/reference/classes/wp_image_editor/ $image_editor = wp_get_image_editor($image_data['file']); if(is_wp_error($image_editor)) { return $image_data; } // Check if the image editor supports the image type if(!$image_edito->supports_mime_type($image_data['type'])) { return $image_data; } // Perform resizing $sizes = $image_editor->get_size(); if((isset($sizes['width']) && $sizes['width'] > $max_width) || (isset($sizes['height']) && $sizes['height'] > $max_height)) { // Resize, but do not crop $image_editor->resize($max_width, $max_height, false); // We will use the default recommended image quality // Change, if you want to set a custom quality //$image_editor->set_quality(90); $saved_image = $image_editor->save($image_data['file']); // TODO return saved image?? } return $image_data; }Also what about the context param? We do not need it, but it should not be left out, for better understanding, hm?
https://developer.ww.wp.xz.cn/reference/hooks/wp_handle_upload/Hi nicohood,
Would you mind opening a new support thread for your questions? We’d like to keep the forum organized as much as we can 🙂
Thank you!
The topic ‘WordPress 5.8 compatibility’ is closed to new replies.