This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
get_image_tag filter
Filters the HTML content for the image tag.
To use the get_image_tag filter, first you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function modify_get_image_tag_defaults which takes 6 parameters and we registered using add_filter. The first parameter get_image_tag is the name of the hook, The second parameter modify_get_image_tag_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use remove_filter to remove the get_image_tag filter.
Parameters
- $html: (string) HTML content for the image.
- $id: (int) Attachment ID.
- $alt: (string) Image description for the alt attribute.
- $title: (string) Image description for the title attribute.
- $align: (string) Part of the class name for aligning the image.
- $size: (string|int[]) Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
Below are the 6 parameters required to use this hook.
Live Example
apply_filters('get_image_tag', string $html, int $id, string $alt, string $title, string $align, string|int[] $size)
Below is an example of how you can use this hook.
Example 1: Basic Usage
This example demonstrates the basic usage of the get_image_tag filter.
function weplugins_modify_get_image_tag_basic($html, $id, $alt, $title, $align, $size) { // Modify the HTML content of the image tag return $html; } add_filter('get_image_tag', 'weplugins_modify_get_image_tag_basic', 10, 6);
Example 2: Conditional Modification
In this example, the HTML content is modified conditionally based on the image ID.
function weplugins_modify_get_image_tag_conditional($html, $id, $alt, $title, $align, $size) { if ($id == 123) { $html = '<img src="new-image.jpg" alt="' . esc_attr($alt) . '" title="' . esc_attr($title) . '" class="align' . esc_attr($align) . '" />'; } return $html; } add_filter('get_image_tag', 'weplugins_modify_get_image_tag_conditional', 10, 6);
Example 3: Removing the Filter
This example shows how to remove the previously added filter.
remove_filter('get_image_tag', 'weplugins_modify_get_image_tag_conditional', 10, 6);
Make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you need any customization or have questions about using this hook, feel free to contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.