wp_ajax_send_attachment_to_editor()
- Since
- 3.5.0
- Source
wp-admin/includes/ajax-actions.php:3320
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.
Compatibility
- WordPress
- since 3.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Performance profile
How much work a call to wp_ajax_send_attachment_to_editor() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 64–105
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 146.
Third-party callbacks on 'media_send_to_editor' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below wp_ajax_send_attachment_to_editor()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 60 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_send_attachment_to_editor() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$url && wp_attachment_is() | 64–72 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && wp_attachment_is() | 66–74 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && wp_attachment_is() | 68–76 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && wp_attachment_is() | 68–76 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && !wp_attachment_is() && empty($url) | 69–80 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
$url | 69–77 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && wp_attachment_is() | 70–78 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && !wp_attachment_is() && empty($url) | 71–82 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
$url | 71–79 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && wp_attachment_is() | 72–80 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && empty($url) | 73–84 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
!$url | 73–81 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
48 further outcomes, up to 103 instructions
$url && !wp_attachment_is() && empty($url) | 73–84 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
$url | 73–81 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && empty($url) | 75–86 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
!$url | 75–83 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && wp_attachment_is() | 76–77 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && empty($url) | 77–88 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
!$url | 77–85 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && wp_attachment_is() | 78–79 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url | 79–92 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
$url && !wp_attachment_is() && !empty($url) | 79–90 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && wp_attachment_is() | 80–81 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && wp_attachment_is() | 80–81 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url | 81–94 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && empty($url) | 81–85 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 81–82 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && !wp_attachment_is() && !empty($url) | 81–92 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && wp_attachment_is() | 82–83 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url | 83–96 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
$url | 83–96 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && !empty($url) | 83–94 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && empty($url) | 83–87 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 83–84 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
$url && !wp_attachment_is() && !empty($url) | 83–94 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && wp_attachment_is() | 84–85 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url | 85–98 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && empty($url) | 85–89 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 85–86 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && !empty($url) | 85–96 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && empty($url) | 85–89 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 85–86 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url | 87–100 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && empty($url) | 87–91 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 87–88 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
!$url && !wp_attachment_is() && !empty($url) | 87–98 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && empty($url) | 89–93 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 89–90 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), stripslashes_deep(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 91–97 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && !empty($url) | 91–95 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 93–99 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && !empty($url) | 93–97 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 95–101 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && $url | 95–101 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && !empty($url) | 95–99 | check_ajax_referer(), wp_unslash(), get_post(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && $url && !wp_attachment_is() && !empty($url) | 95–99 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 97–103 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && !empty($url) | 97–101 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url | 99–105 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), get_image_send_to_editor(), apply_filters(), wp_send_json_success() |
current_user_can() && !$url && !wp_attachment_is() && !empty($url) | 99–103 | check_ajax_referer(), wp_unslash(), get_post(), wp_send_json_error(), wp_send_json_error(), current_user_can(), ID(), get_attachment_link(), remove_filter(), wp_attachment_is(), wp_attachment_is(), esc_url(), apply_filters(), wp_send_json_success() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 146 | 64–105 | 18 | |
| 8.5 | 146 | 64–105 | 18 | |
| 8.4 | 146 | 64–105 | 18 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 154 | 70–113 | 18 | |
| 8.2 | 154 | 70–113 | 18 | |
| 8.1 | 154 | 70–113 | 18 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 157 | 72–114 | 18 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_ajax_send_attachment_to_editor() runs, in this order:
- apply_filters( media_send_to_editor )filterline 3380 (+60 into the body)
Filters the HTML markup for a media item sent to the editor.
Uses · 16
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- get_post()Retrieves post data given a post ID or post object.
- wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
- current_user_can()Returns whether the current user has the specified capability.
- wp_update_post()Updates a post with new post data.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_attachment_link()Retrieves the permalink for an attachment.
- remove_filter()Removes a callback function from a filter hook.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_image_send_to_editor()Retrieves the image HTML to send to the editor.
- wp_attachment_is()Verifies an attachment is of a given type.
Show all 16
- stripslashes_deep()Navigates through an array, object, or scalar, and removes slashes from the values.
- esc_url()Checks and cleans a URL.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
Source code
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 = $attachment['align'] ?? 'none'; $size = $attachment['image-size'] ?? 'medium'; $alt = $attachment['image_alt'] ?? ''; // No whitespace-only captions. $caption = $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 = $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 );}Changelog
Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-admin/includes/ajax-actions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.