wp_ajax_send_attachment_to_editor() WordPress Function

The wp_ajax_send_attachment_to_editor() function is used to send an attachment to the editor. It is called when the user clicks on the "Send to Editor" button in the media uploader.

wp_ajax_send_attachment_to_editor() #

Ajax handler for sending an attachment to the editor.


Description

Generates the HTML to send an attachment to the editor. Backward compatible with the ‘media_send_to_editor’ filter and the chain of filters that follow.


Top ↑

Source

File: wp-admin/includes/ajax-actions.php

3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
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 );
}


Top ↑

Changelog

Changelog
VersionDescription
3.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More