Using WordPress ‘image_send_to_editor_url’ PHP filter

The image_send_to_editor_url WordPress PHP Filter allows you to modify the image URL sent to the editor.

Usage

add_filter('image_send_to_editor_url', 'your_custom_function', 10, 4);
function your_custom_function($html, $src, $alt, $align) {
    // your custom code here
    return $html;
}

Parameters

  • $html (string) – HTML markup sent to the editor for an image.
  • $src (string) – Image source URL.
  • $alt (string) – Image alternate, or alt, text.
  • $align (string) – The image alignment. Default ‘alignnone’. Possible values include ‘alignleft’, ‘aligncenter’, ‘alignright’, ‘alignnone’.

More information

See WordPress Developer Resources: image_send_to_editor_url

Examples

Change image URL

Change the image URL to a CDN domain:

add_filter('image_send_to_editor_url', 'change_image_url', 10, 4);
function change_image_url($html, $src, $alt, $align) {
    $cdn_url = 'https://cdn.example.com';
    $new_src = $cdn_url . $src;
    $html = str_replace($src, $new_src, $html);
    return $html;
}

Add custom CSS class

Add a custom CSS class to the image tag:

add_filter('image_send_to_editor_url', 'add_custom_css_class', 10, 4);
function add_custom_css_class($html, $src, $alt, $align) {
    $custom_class = 'my-custom-class';
    $html = str_replace('<img', '<img class="' . $custom_class . '"', $html);
    return $html;
}

Add custom data attribute

Add a custom data attribute to the image tag:

add_filter('image_send_to_editor_url', 'add_custom_data_attribute', 10, 4);
function add_custom_data_attribute($html, $src, $alt, $align) {
    $custom_data = 'custom-data';
    $html = str_replace('<img', '<img data-custom="' . $custom_data . '"', $html);
    return $html;
}

Change alt text

Modify the alt text of the image:

add_filter('image_send_to_editor_url', 'change_alt_text', 10, 4);
function change_alt_text($html, $src, $alt, $align) {
    $new_alt = 'New Alt Text';
    $html = str_replace('alt="' . $alt . '"', 'alt="' . $new_alt . '"', $html);
    return $html;
}

Change image alignment

Change the image alignment to ‘aligncenter’:

add_filter('image_send_to_editor_url', 'change_image_alignment', 10, 4);
function change_image_alignment($html, $src, $alt, $align) {
    $new_align = 'aligncenter';
    $html = str_replace('class="align' . $align, 'class="align' . $new_align, $html);
    return $html;
}