image_send_to_editor

Home » Hooks » image_send_to_editor

The "image_send_to_editor" hook in WordPress is a powerful tool that allows developers to modify the HTML code generated when an image is inserted into the post editor.

When a user inserts an image into the content of a WordPress post or page, the "image_send_to_editor" hook comes into play. It is triggered just before the image HTML code is sent to the post editor. This provides developers with an opportunity to modify the image’s HTML markup or add additional attributes to the inserted image.

This hook is particularly useful when you want to customize the behavior or appearance of inserted images. For example, you may want to add custom CSS classes to the image, wrap it in a custom HTML container, or even add a data attribute for JavaScript interactions.

To demonstrate the usage of the "image_send_to_editor" hook, let’s consider a scenario where you want to add a custom CSS class to every inserted image. You can achieve this by adding the following code to your theme’s functions.php file:

function modify_inserted_images($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $custom_class = 'custom-image';
    $html = str_replace('class="', 'class="'. $custom_class . ' ', $html);
    return $html;
}
add_filter('image_send_to_editor', 'modify_inserted_images', 10, 8);

In this example, we define a function called "modify_inserted_images" that receives several parameters representing different aspects of the inserted image. We add our desired CSS class "custom-image" to the HTML code using the str_replace function. Finally, we return the modified HTML code.

By attaching the "modify_inserted_images" function to the "image_send_to_editor" hook using the add_filter function, WordPress will now add our custom CSS class to every inserted image.

Remember, the possibilities with the "image_send_to_editor" hook are endless. You can leverage it to achieve various customizations and enhancements to the inserted images in your WordPress website!

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.