Skip to content

Introduce filter to use custom logic to find additional mime types images #311

Closed
@mitogh

Description

@mitogh

The main objective of this filter is to allow third-party plugins or developers to change the current logic used for this plugin (the performance plugin) to locate additional mime images, for example, if a plugin stores the images in a different location and the file names are created out of a custom hash functions rather than in a predictable way, this filter would allow developers to provide the location and logic to replace images in the content of the site.

This is a sub-issue of #160 and related to #307.

Filter

webp_uploads_pre_replace_additional_image_source

Parameters

  • image: The value of the replaced image, if the image is the same as the value returned from the filter the replacement logic is used, meaning that the image was not modified.The HTML code of the image being modified.
  • attachment_id: The ID of the attachment being processed.
  • image_size: The name of the image size we are looking for. When creating the additional mime types for the original image this value would be full, in the same way it occurs in WordPress core.
  • mime_type: The targeted mime type of the file we are looking for.
  • context: Context is under where the image is being used.

Integration

The suggestion location to add this filter are the following places:

Suggested approach

$filtered_image = apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $image_size, $mime_type, $context );
// Only plugin logic to replace the image if the `filtered_image` is the same as the image, meaning it was not modified.
if ( $filtered_image === $image ) {
      $filtered_image = str_replace( ... );
}

Note

We need to make sure the filter runs regardless of whether our own replacement mechanism finds a file.

In other words, some of the conditions in our code need to be checked only after the new filter is run, since those conditions are only relevant for our own way to replace the URLs, but potentially not for how a plugin that stores image files elsewhere would need to replace them.

Metadata

Metadata

Assignees

Labels

[Plugin] Modern Image FormatsIssues for the Modern Image Formats plugin (formerly WebP Uploads)[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions