The media_send_to_editor() WordPress PHP function adds image HTML to the post editor.
Usage
media_send_to_editor( $html );
Example:
Input:
$html = '<img src="image.jpg" alt="Sample Image" />'; media_send_to_editor( $html );
Parameters
- $html (string) – The HTML string of the image that you want to add to the editor.
More information
See WordPress Developer Resources: media_send_to_editor
Examples
Adding a simple image
Insert an image with a specified source and alt text.
$image_html = '<img src="/wp-content/uploads/2023/05/sample-image.jpg" alt="Sample Image" />'; media_send_to_editor( $image_html );
Adding an image with a CSS class
Insert an image with a CSS class for styling purposes.
$image_html = '<img src="/wp-content/uploads/2023/05/sample-image.jpg" class="custom-image" alt="Sample Image" />'; media_send_to_editor( $image_html );
Adding an image with a link
Insert an image wrapped in a link to another page.
$image_html = '<a href="/another-page/"><img src="/wp-content/uploads/2023/05/sample-image.jpg" alt="Sample Image" /></a>'; media_send_to_editor( $image_html );
Adding an image with custom attributes
Insert an image with custom attributes such as width and height.
$image_html = '<img src="/wp-content/uploads/2023/05/sample-image.jpg" alt="Sample Image" width="300" height="200" />'; media_send_to_editor( $image_html );
Adding a responsive image
Insert a responsive image using the srcset
and sizes
attributes.
$image_html = '<img src="/wp-content/uploads/2023/05/sample-image.jpg" alt="Sample Image" srcset="/wp-content/uploads/2023/05/sample-image-300x200.jpg 300w, /wp-content/uploads/2023/05/sample-image-600x400.jpg 600w" sizes="(max-width: 600px) 100vw, 600px" />'; media_send_to_editor( $image_html );