Title: wp_ajax_send_attachment_to_editor
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_ajax_send_attachment_to_editor()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#wp--skip-link--target)

Handles sending an attachment to the editor via AJAX.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#description)󠁿

Generates the HTML to send an attachment to the editor.
Backward compatible with
the [‘media_send_to_editor’](https://developer.wordpress.org/reference/hooks/media_send_to_editor/)
filter and the chain of filters that follow.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#source)󠁿

    ```php
    function wp_ajax_send_attachment_to_editor() {
    	check_ajax_referer( 'media-send-to-editor', 'nonce' );

    	$attachment = wp_unslash( $_POST['attachment'] );

    	$id = (int) $attachment['id'];

    	$post = get_post( $id );
    	if ( ! $post ) {
    		wp_send_json_error();
    	}

    	if ( 'attachment' !== $post->post_type ) {
    		wp_send_json_error();
    	}

    	if ( current_user_can( 'edit_post', $id ) ) {
    		// If this attachment is unattached, attach it. Primarily a back compat thing.
    		$insert_into_post_id = (int) $_POST['post_id'];

    		if ( 0 === $post->post_parent && $insert_into_post_id ) {
    			wp_update_post(
    				array(
    					'ID'          => $id,
    					'post_parent' => $insert_into_post_id,
    				)
    			);
    		}
    	}

    	$url = empty( $attachment['url'] ) ? '' : $attachment['url'];
    	$rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url );

    	remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

    	if ( str_starts_with( $post->post_mime_type, 'image' ) ) {
    		$align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
    		$size  = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
    		$alt   = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';

    		// No whitespace-only captions.
    		$caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
    		if ( '' === trim( $caption ) ) {
    			$caption = '';
    		}

    		$title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
    		$html  = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt );
    	} elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) {
    		$html = stripslashes_deep( $_POST['html'] );
    	} else {
    		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
    		$rel  = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized.

    		if ( ! empty( $url ) ) {
    			$html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
    		}
    	}

    	/** This filter is documented in wp-admin/includes/media.php */
    	$html = apply_filters( 'media_send_to_editor', $html, $id, $attachment );

    	wp_send_json_success( $html );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L3313)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L3313-L3376)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#hooks)󠁿

 [apply_filters( ‘media_send_to_editor’, string $html, int $send_id, array $attachment )](https://developer.wordpress.org/reference/hooks/media_send_to_editor/)

Filters the HTML markup for a media item sent to the editor.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_attachment_is()](https://developer.wordpress.org/reference/functions/wp_attachment_is/)`wp-includes/post.php` |

Verifies an attachment is of a given type.

  | 
| [get_image_send_to_editor()](https://developer.wordpress.org/reference/functions/get_image_send_to_editor/)`wp-admin/includes/media.php` |

Retrieves the image HTML to send to the editor.

  | 
| [stripslashes_deep()](https://developer.wordpress.org/reference/functions/stripslashes_deep/)`wp-includes/formatting.php` |

Navigates through an array, object, or scalar, and removes slashes from the values.

  | 
| [get_attachment_link()](https://developer.wordpress.org/reference/functions/get_attachment_link/)`wp-includes/link-template.php` |

Retrieves the permalink for an attachment.

  | 
| [wp_update_post()](https://developer.wordpress.org/reference/functions/wp_update_post/)`wp-includes/post.php` |

Updates a post with new post data.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [remove_filter()](https://developer.wordpress.org/reference/functions/remove_filter/)`wp-includes/plugin.php` |

Removes a callback function from a filter hook.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 9 more](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.5.0](https://developer.wordpress.org/reference/since/3.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_send_attachment_to_editor%2F)
before being able to contribute a note or feedback.