Using WordPress ‘image_media_send_to_editor()’ PHP function

The image_media_send_to_editor WordPress PHP function retrieves the media element HTML to send to the editor.

Usage

$image_html = image_media_send_to_editor($html, $attachment_id, $attachment);

Parameters

  • $html (string) – The HTML markup for the media element.
  • $attachment_id (int) – The ID of the attachment.
  • $attachment (array) – An array containing attachment data.

More information

See WordPress Developer Resources: image_media_send_to_editor

Examples

Display an Image in the Editor

This example displays an image with the given attachment ID in the editor.

// Set the attachment ID
$attachment_id = 25;

// Get attachment data
$attachment = wp_prepare_attachment_for_js($attachment_id);

// Create image HTML markup
$html = wp_get_attachment_image($attachment_id, 'medium');

// Send image to the editor
$image_html = image_media_send_to_editor($html, $attachment_id, $attachment);

// Output the image HTML
echo $image_html;

This example displays a gallery with the given attachment IDs in the editor.

// Set the attachment IDs
$attachment_ids = array(25, 26, 27);

// Create gallery shortcode
$shortcode = '';

// Send gallery to the editor
$gallery_html = image_media_send_to_editor($shortcode, 0, array());

// Output the gallery HTML
echo $gallery_html;

Display a Video in the Editor

This example displays a video with the given attachment ID in the editor.

// Set the attachment ID
$attachment_id = 28;

// Get attachment data
$attachment = wp_prepare_attachment_for_js($attachment_id);

// Create video HTML markup
$html = wp_video_shortcode(array('src' => $attachment['url']));

// Send video to the editor
$video_html = image_media_send_to_editor($html, $attachment_id, $attachment);

// Output the video HTML
echo $video_html;

Display an Audio File in the Editor

This example displays an audio file with the given attachment ID in the editor.

// Set the attachment ID
$attachment_id = 29;

// Get attachment data
$attachment = wp_prepare_attachment_for_js($attachment_id);

// Create audio HTML markup
$html = wp_audio_shortcode(array('src' => $attachment['url']));

// Send audio to the editor
$audio_html = image_media_send_to_editor($html, $attachment_id, $attachment);

// Output the audio HTML
echo $audio_html;

Display a Custom Media Element in the Editor

This example displays a custom media element with the given attachment ID in the editor.

// Set the attachment ID
$attachment_id = 30;

// Get attachment data
$attachment = wp_prepare_attachment_for_js($attachment_id);

// Create custom media element HTML markup
$html = '<div class="custom-media-element">' . wp_get_attachment_image($attachment_id, 'medium') . '</div>';

// Send custom media element to the editor
$custom_media_html = image_media_send_to_editor($html, $attachment_id, $attachment);

// Output the custom media element HTML
echo $custom_media_html;