wp_ajax_send_link_to_editor

The timeline below displays how wordpress function wp_ajax_send_link_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 a link to the editor via AJAX.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post     Global post object.
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    $src = wp_unslash($_POST['src']);
    if (!$src) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    $src = sanitize_url($src);
    if (!$src) {
        wp_send_json_error();
    }
    $link_text = trim(wp_unslash($_POST['link_text']));
    if (!$link_text) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this.
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src);
    if ($ext) {
        $ext_type = wp_ext2type($ext);
        if ('audio' === $ext_type || 'video' === $ext_type) {
            $type = $ext_type;
        }
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 6.1

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post     Global post object.
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    $src = wp_unslash($_POST['src']);
    if (!$src) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    $src = sanitize_url($src);
    if (!$src) {
        wp_send_json_error();
    }
    $link_text = trim(wp_unslash($_POST['link_text']));
    if (!$link_text) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this.
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src);
    if ($ext) {
        $ext_type = wp_ext2type($ext);
        if ('audio' === $ext_type || 'video' === $ext_type) {
            $type = $ext_type;
        }
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 5.5

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post     Global post object.
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    $src = wp_unslash($_POST['src']);
    if (!$src) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    $src = esc_url_raw($src);
    if (!$src) {
        wp_send_json_error();
    }
    $link_text = trim(wp_unslash($_POST['link_text']));
    if (!$link_text) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this.
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src);
    if ($ext) {
        $ext_type = wp_ext2type($ext);
        if ('audio' === $ext_type || 'video' === $ext_type) {
            $type = $ext_type;
        }
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 5.4

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post     Global post object.
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    $src = wp_unslash($_POST['src']);
    if (!$src) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    $src = esc_url_raw($src);
    if (!$src) {
        wp_send_json_error();
    }
    $link_text = trim(wp_unslash($_POST['link_text']));
    if (!$link_text) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this.
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src);
    if ($ext) {
        $ext_type = wp_ext2type($ext);
        if ('audio' == $ext_type || 'video' == $ext_type) {
            $type = $ext_type;
        }
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 5.3

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post     Global post object.
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    $src = wp_unslash($_POST['src']);
    if (!$src) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    $src = esc_url_raw($src);
    if (!$src) {
        wp_send_json_error();
    }
    $link_text = trim(wp_unslash($_POST['link_text']));
    if (!$link_text) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src);
    if ($ext) {
        $ext_type = wp_ext2type($ext);
        if ('audio' == $ext_type || 'video' == $ext_type) {
            $type = $ext_type;
        }
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 4.8

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$link_text = trim(wp_unslash($_POST['link_text']))) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters("{$type}_send_to_editor_url", $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 4.6

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backward compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$link_text = trim(wp_unslash($_POST['link_text']))) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters($type . '_send_to_editor_url', $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 4.3

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backwards compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 *
 * @global WP_Post  $post
 * @global WP_Embed $wp_embed
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$link_text = trim(wp_unslash($_POST['link_text']))) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters($type . '_send_to_editor_url', $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 4.2

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backwards compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$link_text = trim(wp_unslash($_POST['link_text']))) {
        $link_text = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($link_text) {
        $html = '<a href="' . esc_url($src) . '">' . $link_text . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters($type . '_send_to_editor_url', $html, $src, $link_text);
    wp_send_json_success($html);
}

WordPress Version: 4.0

/**
 * Ajax handler for sending a link to the editor.
 *
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backwards compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 */
function wp_ajax_send_link_to_editor()
{
    global $post, $wp_embed;
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$title = trim(wp_unslash($_POST['title']))) {
        $title = wp_basename($src);
    }
    $post = get_post(isset($_POST['post_id']) ? $_POST['post_id'] : 0);
    // Ping WordPress for an embed.
    $check_embed = $wp_embed->run_shortcode('[embed]' . $src . '[/embed]');
    // Fallback that WordPress creates when no oEmbed was found.
    $fallback = $wp_embed->maybe_make_link($src);
    if ($check_embed !== $fallback) {
        // TinyMCE view for [embed] will parse this
        $html = '[embed]' . $src . '[/embed]';
    } elseif ($title) {
        $html = '<a href="' . esc_url($src) . '">' . $title . '</a>';
    } else {
        $html = '';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters($type . '_send_to_editor_url', $html, $src, $title);
    wp_send_json_success($html);
}

WordPress Version: 3.7

/**
 * Generates the HTML to send a non-image embed link to the editor.
 *
 * Backwards compatible with the following filters:
 * - file_send_to_editor_url
 * - audio_send_to_editor_url
 * - video_send_to_editor_url
 *
 * @since 3.5.0
 */
function wp_ajax_send_link_to_editor()
{
    check_ajax_referer('media-send-to-editor', 'nonce');
    if (!$src = wp_unslash($_POST['src'])) {
        wp_send_json_error();
    }
    if (!strpos($src, '://')) {
        $src = 'http://' . $src;
    }
    if (!$src = esc_url_raw($src)) {
        wp_send_json_error();
    }
    if (!$title = trim(wp_unslash($_POST['title']))) {
        $title = wp_basename($src);
    }
    $html = '';
    if ($title) {
        $html = '<a href="' . esc_url($src) . '">' . $title . '</a>';
    }
    // Figure out what filter to run:
    $type = 'file';
    if (($ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $src)) && ($ext_type = wp_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
        $type = $ext_type;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $html = apply_filters($type . '_send_to_editor_url', $html, $src, $title);
    wp_send_json_success($html);
}