Title: New filter to modify content images in WordPress 6.0
Author: Paul Bearne
Published: April 27, 2022
Last modified: May 3, 2022

---

 [  ](https://profiles.wordpress.org/pbearne/) [Paul Bearne](https://profiles.wordpress.org/pbearne/)
6:32 pm _on_ April 27, 2022     
Tags: [6.0 ( 76 )](https://make.wordpress.org/core/tag/6-0/),
[dev-notes ( 650 )](https://make.wordpress.org/core/tag/dev-notes/), [dev-notes-6.0 ( 20 )](https://make.wordpress.org/core/tag/dev-notes-6-0/),
[performance ( 420 )](https://make.wordpress.org/core/tag/performance/)   

# New filter to modify content images in WordPress 6.0

WordPress 6.0 introduces a new filterFilter Filters are one of the two types of 
Hooks [https://codex.wordpress.org/Plugin_API/Hooks](https://codex.wordpress.org/Plugin_API/Hooks).
They provide a way for functions to modify data of other functions. They are the
counterpart to Actions. Unlike Actions, filters are meant to work in an isolated
manner, and should never have side effects such as affecting global variables and
output.  `wp_content_img_tag` to allow adjustment of images in a blob of HTMLHTML
HyperText Markup Language. The semantic scripting language primarily used for outputting
content in web browsers. content, most prominently post content via `the_content`
filter.

WordPress 5.5 originally introduced the `wp_filter_content_tags()` function to modify
certain elements, primarily images, in a blob of HTML content. Prior to the WordPress
6.0 release, it was not possible to alter these image tags without duplicating the
complex regex logic in the `wp_filter_content_tags()` function. This increased complexity
and overhead. The new `wp_content_img_tag` filter solves this problem.

## How to use the filter

The new `wp_content_img_tag` filter passes the following parameters:

 * string `$filtered_image`: The full img tagtag A directory in Subversion. WordPress
   uses tags to store a single snapshot of a version (3.6, 3.6.1, etc.), the common
   convention of tags in version control systems. (Not to be confused with post 
   tags.) with attributes that will replace the source image tag.
 * string `$context`: Additional context, like the current filter name or the function
   name from where this was called.
 * int `$attachment_id`: The image attachment ID. May be 0 in case the image is 
   not an attachment.

The filter must return a string which will then replace the `img` tag passed to 
the filter.

### Example

Here is an example in which the new filter is used to add a border-color style attribute
to every image tag in the content.

    ```notranslate
    function myplugin_img_tag_add_border_color( $filtered_image, $context, $attachment_id ) {
    	$style = 'border-color: #cccccc;';

    	$filtered_image = str_replace( '<img ', '<img style="' . $style . '" ', $filtered_image );

    	return $filtered_image;
    }
    add_filter( 'wp_content_img_tag', 'myplugin_img_tag_add_border_color', 10, 3 );
    ```

---

[The `wp_filter_content_tags()` function was originally introduced to facilitate lazy-loading support for images](https://make.wordpress.org/core/2020/07/14/lazy-loading-images-in-5-5/)
and has since become the standard approach for modifying in-content images for various
performance enhancements. The new `wp_content_img_tag` filter expands these capabilitiescapability
A **capability** is permission to perform one or more types of task. Checking if
a user has a capability is performed by the `current_user_can` function. Each user
of a WordPress site might have some permissions but not others, depending on their
role. For example, users who have the Author role usually have permission to edit
their own posts (the “edit_posts” capability), but not permission to edit other 
users’ posts (the “edit_others_posts” capability). by giving control to pluginPlugin
A plugin is a piece of software containing a group of functions that can be added
to a WordPress website. They can extend functionality or add new features to your
WordPress websites. WordPress plugins are written in the PHP programming language
and integrate seamlessly with WordPress. These can be free in the WordPress.org 
Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
or can be cost-based plugin from a third-party. developers to add their own customizations.

For more context on the new filter, implementation see [#55347](https://core.trac.wordpress.org/ticket/55347).

_Props to [@flixos90](https://profiles.wordpress.org/flixos90/) , [@kkautzman](https://profiles.wordpress.org/kkautzman/),
and [@milana\_cap](https://profiles.wordpress.org/milana_cap/) for review and proofreading._

[#6-0](https://make.wordpress.org/core/tag/6-0/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/),
[#dev-notes-6-0](https://make.wordpress.org/core/tag/dev-notes-6-0/), [#performance](https://make.wordpress.org/core/tag/performance/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2022/04/27/new-filter-to-modify-content-images-in-wordpress-6-0/?share=linkedin)

# Post navigation

[← Exploration to enable better Developer and Visitor Experiences with blocks](https://make.wordpress.org/core/2022/04/27/exploration-to-enable-better-developer-and-visitor-experiences-with-blocks/)

[Changes to do_parse_request filter in WordPress 6.0 →](https://make.wordpress.org/core/2022/04/27/changes-to-do_parse_request-filter-in-wordpress-6-0/)