The media_send_to_editor WordPress PHP filter allows you to modify the HTML markup for a media item before it is sent to the editor.
Usage
add_filter('media_send_to_editor', 'my_custom_function', 10, 3); function my_custom_function($html, $send_id, $attachment) { // your custom code here return $html; }
Parameters
$html
(string) – The HTML markup for a media item sent to the editor.$send_id
(int) – The first key from the$_POST['send']
data.$attachment
(array) – Array of attachment metadata.
More information
See WordPress Developer Resources: media_send_to_editor
Examples
Adding a CSS class to the image tag
This example adds a custom CSS class to the image tag before it is sent to the editor.
add_filter('media_send_to_editor', 'add_custom_class_to_image', 10, 3); function add_custom_class_to_image($html, $send_id, $attachment) { $html = str_replace('<img', '<img class="my-custom-class"', $html); return $html; }
Wrapping media item in a div
This example wraps the media item in a div with a custom class.
add_filter('media_send_to_editor', 'wrap_media_in_div', 10, 3); function wrap_media_in_div($html, $send_id, $attachment) { $html = '<div class="my-custom-wrapper">' . $html . '</div>'; return $html; }
Adding a data attribute to the media item
This example adds a custom data attribute to the media item.
add_filter('media_send_to_editor', 'add_data_attribute', 10, 3); function add_data_attribute($html, $send_id, $attachment) { $html = str_replace('<img', '<img data-custom-attribute="my-value"', $html); return $html; }
Adding a caption to the media item
This example adds a caption to the media item if available in the attachment metadata.
add_filter('media_send_to_editor', 'add_caption_to_media', 10, 3); function add_caption_to_media($html, $send_id, $attachment) { if (!empty($attachment['caption'])) { $html = '<figure>' . $html . '<figcaption>' . $attachment['caption'] . '</figcaption></figure>'; } return $html; }
Replacing media item with a custom shortcode
This example replaces the media item with a custom shortcode that includes the attachment ID.
add_filter('media_send_to_editor', 'replace_media_with_shortcode', 10, 3); function replace_media_with_shortcode($html, $send_id, $attachment) { $html = '[my_custom_shortcode id="' . $send_id . '"]'; return $html; }