wp_ajax_send_attachment_to_editor

The timeline below displays how wordpress function wp_ajax_send_attachment_to_editor has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles sending an attachment to the editor via AJAX.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
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);
}

WordPress Version: 5.6

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
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 = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 5.5

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($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 = intval($_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 = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 5.4

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($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 = intval($_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 = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 5.3

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($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 = intval($_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 = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 4.6

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backward compatible with the {@see 'media_send_to_editor'} filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $url = empty($attachment['url']) ? '' : $attachment['url'];
    $rel = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 5.1

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $url = empty($attachment['url']) ? '' : $attachment['url'];
    $rel = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 4.5

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $url = empty($attachment['url']) ? '' : $attachment['url'];
    $rel = strpos($url, 'attachment_id') || get_attachment_link($id) == $url;
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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);
}

WordPress Version: 4.4

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $rel = '';
    $url = empty($attachment['url']) ? '' : $attachment['url'];
    if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) {
        $rel = 'attachment wp-att-' . $id;
    }
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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'] : '';
        if (!empty($url)) {
            $html = '<a href="' . esc_url($url) . '"' . 'rel="' . esc_attr($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);
}

WordPress Version: 4.2

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $rel = $url = '';
    $html = isset($attachment['post_title']) ? $attachment['post_title'] : '';
    if (!empty($attachment['url'])) {
        $url = $attachment['url'];
        if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) {
            $rel = ' rel="attachment wp-att-' . $id . '"';
        }
        $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>';
    }
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $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, (bool) $rel, $size, $alt);
    } elseif (wp_attachment_is('video', $post) || wp_attachment_is('audio', $post)) {
        $html = stripslashes_deep($_POST['html']);
    }
    /** 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);
}

WordPress Version: 4.0

/**
 * Ajax handler for sending an attachment to the editor.
 *
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter
 * and the chain of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $rel = $url = '';
    $html = isset($attachment['post_title']) ? $attachment['post_title'] : '';
    if (!empty($attachment['url'])) {
        $url = $attachment['url'];
        if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) {
            $rel = ' rel="attachment wp-att-' . $id . '"';
        }
        $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>';
    }
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $align = isset($attachment['align']) ? $attachment['align'] : 'none';
        $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium';
        $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : '';
        $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : '';
        $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, (bool) $rel, $size, $alt);
    } elseif ('video' === substr($post->post_mime_type, 0, 5) || 'audio' === substr($post->post_mime_type, 0, 5)) {
        $html = stripslashes_deep($_POST['html']);
    }
    /** 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);
}

WordPress Version: 3.7

/**
 * Generates the HTML to send an attachment to the editor.
 * Backwards compatible with the media_send_to_editor filter and the chain
 * of filters that follow.
 *
 * @since 3.5.0
 */
function wp_ajax_send_attachment_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    $attachment = wp_unslash($_POST['attachment']);
    $id = intval($attachment['id']);
    if (!$post = get_post($id)) {
        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.
        if (0 == $post->post_parent && $insert_into_post_id = intval($_POST['post_id'])) {
            wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id));
        }
    }
    $rel = $url = '';
    $html = $title = isset($attachment['post_title']) ? $attachment['post_title'] : '';
    if (!empty($attachment['url'])) {
        $url = $attachment['url'];
        if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) {
            $rel = ' rel="attachment wp-att-' . $id . '"';
        }
        $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>';
    }
    remove_filter('media_send_to_editor', 'image_media_send_to_editor');
    if ('image' === substr($post->post_mime_type, 0, 5)) {
        $align = isset($attachment['align']) ? $attachment['align'] : 'none';
        $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium';
        $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : '';
        $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : '';
        $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, (bool) $rel, $size, $alt);
    } elseif ('video' === substr($post->post_mime_type, 0, 5) || 'audio' === substr($post->post_mime_type, 0, 5)) {
        $html = stripslashes_deep($_POST['html']);
    }
    /** 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);
}