Archived topic
webp
7 replies · Started by Howard on November 29, 2020
Hi:
Would this be proper code to enter WEB P image to generatepress theme editor?
//** *Enable upload for webp image files.*/
function webp_upload_mimes($existing_mimes) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
Also: would it just be pasted on the NEXT LINE AT THE BOTTOM of theme editor and saved?
Thanks
Howard
Hi,
Yes your PHP snippet is pretty close, but it seems to be missing the filter where its suppose to apply to.
Its also missing a } to close the function which is pretty important as it can cause syntax errors.
Here's a more complete version of your code:
//** *Enable upload for webp image files.*/
function webp_upload_mimes($existing_mimes) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');
//** * Enable preview / thumbnail for webp image files.*/
function webp_is_displayable($result, $path) {
if ($result === false) {
$displayable_image_types = array( IMAGETYPE_WEBP );
$info = @getimagesize( $path );
if (empty($info)) {
$result = false;
} elseif (!in_array($info[2], $displayable_image_types)) {
$result = false;
} else {
$result = true;
}
}
return $result;
}
add_filter('file_is_displayable_image', 'webp_is_displayable', 10, 2);
Please refer to this on how to add PHP snippets to your site - https://docs.generatepress.com/article/adding-php/
Thanks- I will just paste it to my “Code Snippets” app
Howard
No problem. :)
Hi
Another question
( more complicated)
How to fix:
Ensure text remains visible during webfont load
Leverage the font-display CSS feature to ensure text is user-visible while webfonts are loading.
I know there are code(s) ..
But where and how to apply them ( novice)
Howard
Hi,
Let's try to keep it 1 issue per topic.
That said, can you open up a new one for your other questions? Thank you. :)
Hi: This code does not seem to work when put into CODE SNIPPETS
//** *Enable upload for webp image files.*/
function webp_upload_mimes($existing_mimes) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');
//** * Enable preview / thumbnail for webp image files.*/
function webp_is_displayable($result, $path) {
if ($result === false) {
$displayable_image_types = array( IMAGETYPE_WEBP );
$info = @getimagesize( $path );
if (empty($info)) {
$result = false;
} elseif (!in_array($info[2], $displayable_image_types)) {
$result = false;
} else {
$result = true;
}
}
return $result;
}
add_filter('file_is_displayable_image
Hi,
This part seems to be missing some codes.
...
add_filter(‘file_is_displayable_image
You have to add the function name(webp_is_displayable), priority (10) and # of accepted args(2).
add_filter('file_is_displayable_image', 'webp_is_displayable', 10, 2);
Also, be mindful with smart apostrophe/curly quotes ‘. It doesn't work if you use that, straight quote should be used '.