wp_ajax_set_post_thumbnail()
- Since
- 3.1.0
- Source
wp-admin/includes/ajax-actions.php:2736
Compatibility
- WordPress
- since 3.1.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_set_post_thumbnail() 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
- 30–63
- Plugin surface
- None
- 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 78.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_ajax_set_post_thumbnail() - querycontent query
get_post()one call below wp_ajax_set_post_thumbnail()
Further down the call graph this can also reach option, serialize, cache 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 · 24 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_set_post_thumbnail() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() && $thumbnail_id !== -1 && !set_post_thumbnail() | 30–31 | current_user_can(), check_ajax_referer(), set_post_thumbnail(), wp_die() |
!current_user_can() && $thumbnail_id !== -1 && !set_post_thumbnail() | 33–34 | current_user_can(), wp_die(), check_ajax_referer(), set_post_thumbnail(), wp_die() |
current_user_can() && $thumbnail_id === -1 && !delete_post_thumbnail() && !set_post_thumbnail() | 37–38 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && !delete_post_thumbnail() && !set_post_thumbnail() | 40–41 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), wp_die() |
current_user_can() && $value && $thumbnail_id !== -1 && set_post_thumbnail() | 41–42 | current_user_can(), check_ajax_referer(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
current_user_can() && $thumbnail_id !== -1 && set_post_thumbnail() && !$value | 42–43 | current_user_can(), check_ajax_referer(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
!current_user_can() && $value && $thumbnail_id !== -1 && set_post_thumbnail() | 44–45 | current_user_can(), wp_die(), check_ajax_referer(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
!current_user_can() && $thumbnail_id !== -1 && set_post_thumbnail() && !$value | 45–46 | current_user_can(), wp_die(), check_ajax_referer(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
current_user_can() && $value && $thumbnail_id === -1 && delete_post_thumbnail() && !set_post_thumbnail() | 46–47 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), wp_die() |
current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && !$value && !set_post_thumbnail() | 47–48 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), wp_die() |
current_user_can() && $value && $thumbnail_id === -1 && !delete_post_thumbnail() && set_post_thumbnail() | 48–49 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
current_user_can() && $thumbnail_id === -1 && !delete_post_thumbnail() && set_post_thumbnail() && !$value | 49–50 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
12 further outcomes, up to 63 instructions
!current_user_can() && $value && $thumbnail_id === -1 && delete_post_thumbnail() && !set_post_thumbnail() | 49–50 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && !$value && !set_post_thumbnail() | 50–51 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), wp_die() |
!current_user_can() && $value && $thumbnail_id === -1 && !delete_post_thumbnail() && set_post_thumbnail() | 51–52 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && !delete_post_thumbnail() && set_post_thumbnail() && !$value | 52–53 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
current_user_can() && $value && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 57–58 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 58–59 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 58–59 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && !$value && set_post_thumbnail() | 59–60 | current_user_can(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
!current_user_can() && $value && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 60–61 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 61–62 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && set_post_thumbnail() | 61–62 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_die(), wp_die() |
!current_user_can() && $thumbnail_id === -1 && delete_post_thumbnail() && !$value && set_post_thumbnail() | 62–63 | current_user_can(), wp_die(), check_ajax_referer(), delete_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), set_post_thumbnail(), _wp_post_thumbnail_html(), wp_send_json_success(), wp_die() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 74 | 30–61 | 7 | 4 fewer instructions than PHP 8.5 |
| 8.5 | 78 | 30–63 | 7 | |
| 8.4 | 78 | 30–63 | 7 | |
| 8.3 | 78 | 30–63 | 7 | |
| 8.2 | 78 | 30–63 | 7 | |
| 8.1 | 78 | 30–63 | 7 | |
| 7.4 | 78 | 30–63 | 7 |
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.
Uses · 7
- current_user_can()Returns whether the current user has the specified capability.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- delete_post_thumbnail()Removes the thumbnail (featured image) from the given post.
- _wp_post_thumbnail_html()Returns HTML for the post thumbnail meta box.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
- set_post_thumbnail()Sets the post thumbnail (featured image) for the given post.
Source code
function wp_ajax_set_post_thumbnail() { $json = ! empty( $_REQUEST['json'] ); // New-style request. $post_id = (int) $_POST['post_id']; if ( ! current_user_can( 'edit_post', $post_id ) ) { wp_die( -1 ); } $thumbnail_id = (int) $_POST['thumbnail_id']; if ( $json ) { check_ajax_referer( "update-post_$post_id" ); } else { check_ajax_referer( "set_post_thumbnail-$post_id" ); } if ( -1 === $thumbnail_id ) { if ( delete_post_thumbnail( $post_id ) ) { $return = _wp_post_thumbnail_html( null, $post_id ); $json ? wp_send_json_success( $return ) : wp_die( $return ); } else { wp_die( 0 ); } } if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) { $return = _wp_post_thumbnail_html( $thumbnail_id, $post_id ); $json ? wp_send_json_success( $return ) : wp_die( $return ); } wp_die( 0 );}Changelog
Introduced in 3.1.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 6.9.7 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.