Convert your WordPress JPG/PNG images to WebP formats during runtime.
Download from WordPress plugin repository.
You can also get the latest version from any of our release tags.
As an internet user, you already know images can be the difference between a great website experience and a terrible one! Think about how often you've landed on a website and hit the back button because the home page was too busy or the banner image was taking so much time to load due to its size.
You may not realize it, but imagery is a large part of it. This plugin helps take care of all those concerns, by converting your WordPress images to WebP format during page load so that your site loads extremely fast, without any disruptions or downtime.
icfw_options
This custom hook (filter) provides the ability to add custom options for your image conversions to WebP. For e.g. to perform a 50% quality, image conversion using the Imagick extension, you could do:
add_filter( 'icfw_options', [ $this, 'custom_options' ] );
public function custom_options( $options ): array {
$options = wp_parse_args(
[
'quality' => 50,
'converter' => 'imagick',
],
$options
);
return (array) $options;
}
Parameters
{array}
By default this will be an associative array containing key, value options of each image conversion.
icfw_convert
This custom hook (action) fires immediately after the image is converted to WebP. For e.g. you can capture errors to a custom post type of yours like so:
add_action( 'icfw_convert', [ $this, 'log_webp_errors' ], 10, 2 );
public function log_webp_errors( $webp, $attachment_id ): void {
if ( is_wp_error( $webp ) ) {
wp_insert_post(
[
'post_type' => 'webp_error',
'post_title' => (string) $webp->get_error_message(),
'post_status' => 'publish',
]
)
}
}
Parameters
{string|WP_Error}
_ By default this will be the WebP return value after an image conversion is done. If successful, a string is returned, otherwise a WP_Error instance is.{int}
_ By default this is the Image ID.
icfw_attachment_html
This custom hook (filter) provides the ability to modify the resulting WebP image HTML. For e.g. you can nest your image HTML into a figure element like so:
add_filter( 'icfw_attachment_html', [ $this, 'custom_img_html' ], 10, 2 );
public function custom_img_html( $html, $attachment_id ): string {
return sprintf(
'<figure>
%s
<figcaption>Image ID: %s</figcaption>
</figure>',
(string) $html,
(string) $attchment_id
);
}
Parameters
{string}
By default this will be the image HTML.{int}
_ By default this is the Image ID.
icfw_form_fields
This custom hook (filter) provides the ability to add custom fields to the Admin options page like so:
add_filter( 'icfw_form_fields', [ $this, 'custom_form_fields' ] );
public function custom_form_fields( $fields ): array {
$fields = wp_parse_args(
[
'custom_group' => [
'label' => 'Custom Heading',
'controls' => [
'custom_option_1' => [
'control' => 'text',
'label' => 'My Custom Option 1',
'summary' => 'Enable this option to save my custom option 1.',
],
'custom_option_2' => [
'control' => 'select',
'label' => 'My Custom Option 2',
'summary' => 'Enable this option to save my custom option 2.',
'options' => [],
],
'custom_option_3' => [
'control' => 'checkbox',
'label' => 'My Custom Option 3',
'summary' => 'Enable this option to save my custom option 3.',
],
],
],
],
$fields
);
return (array) $fields;
}
Parameters
{array}
By default this will be an associative array containing key, value options of each field option.
icfw_thumbnail_html
This custom hook (filter) provides the ability to modify the resulting WebP image HTML. For e.g. you can nest your image HTML into a figure element like so:
add_filter( 'icfw_thumbnail_html', [ $this, 'custom_img_html' ], 10, 2 );
public function custom_img_html( $html, $thumbnail_id ): string {
return sprintf(
'<figure>
%s
<figcaption>Image ID: %s</figcaption>
</figure>',
(string) $html,
(string) $thumbnail_id
);
}
Parameters
{string}
By default this will be the image HTML.{int}
_ By default this is the Image ID.
icfw_delete
This custom hook (action) fires immediately after a WebP image is deleteed.
add_action( 'icfw_delete', [ $this, 'delete_bmp_image' ], 10, 2 );
public function delete_bmp_image( $webp, $attachment_id ): void {
$bmp = str_replace( '.webp', '.bmp', $webp );
if ( file_exists( $bmp ) ) {
unlink( $bmp );
}
}
Parameters
{string}
By default this will be the absolute path of the WebP image.{int}
_ By default this is the Image ID.
icfw_metadata_delete
This custom hook (action) fires immediately after a WebP metadata image is deleteed.
add_action( 'icfw_metadata_delete', [ $this, 'delete_bmp_image' ], 10, 2 );
public function delete_bmp_image( $webp, $attachment_id ): void {
$bmp = str_replace( '.webp', '.bmp', $webp );
if ( file_exists( $bmp ) ) {
unlink( $bmp );
}
}
Parameters
{string}
By default this will be the absolute path of the WebP metadata image.{int}
_ By default this is the Image ID.
v7.4|v8.0
installed in your computer.composer install
to build PHP dependencies.# Run PHP Linting.
composer run lint
# Fix PHP Linting errors.
composer run lint:fix
composer run test
composer run analyse
First, thank you for taking the time to contribute!
Contributing isn't just writing code - it's anything that improves the project. All contributions are managed right here on Github. Here are some ways you can help:
If you're running into an issue, please take a look through existing issues and open a new one if needed. If you're able, include steps to reproduce, environment information, and screenshots/screencasts as relevant. To create a branch that fixes a bug, please use the convention fix/{issue-number}-your-branch-name
like so:
git checkout -b fix/1234-image-mime-type-bug
New features and enhancements are also managed via issues. To create a branch that adds a feature, please use the convention feat/{issue-number}-your-branch-name
like so:
git checkout -b feat/1234-image-error-logging-capability
Pull requests represent a proposed solution to a specified problem. They should always reference an issue that describes the problem in detail. Discussion on pull requests should be limited to the pull request, i.e. code review.