Title: wp_ajax_set_post_thumbnail
Published: April 25, 2014
Last modified: May 20, 2026

---

# wp_ajax_set_post_thumbnail()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#wp--skip-link--target)

Handles setting the featured image via AJAX.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/ajax-actions.php#L2743)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/ajax-actions.php#L2743-L2774)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#related)󠁿

| Uses | Description | 
| [_wp_post_thumbnail_html()](https://developer.wordpress.org/reference/functions/_wp_post_thumbnail_html/)`wp-admin/includes/post.php` |

Returns HTML for the post thumbnail meta box.

  | 
| [delete_post_thumbnail()](https://developer.wordpress.org/reference/functions/delete_post_thumbnail/)`wp-includes/post.php` |

Removes the thumbnail (featured image) from the given post.

  | 
| [set_post_thumbnail()](https://developer.wordpress.org/reference/functions/set_post_thumbnail/)`wp-includes/post.php` |

Sets the post thumbnail (featured image) for the given post.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [wp_die()](https://developer.wordpress.org/reference/functions/wp_die/)`wp-includes/functions.php` |

Kills WordPress execution and displays HTML page with an error message.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.1.0](https://developer.wordpress.org/reference/since/3.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_set_post_thumbnail%2F)
before being able to contribute a note or feedback.