wp_ajax_set_post_thumbnail

Ajax handler for setting the featured image.

Description

wp_ajax_set_post_thumbnail(); 

Usage

  1. if ( !function_exists( 'wp_ajax_set_post_thumbnail' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_set_post_thumbnail(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_set_post_thumbnail() { 
  2. $json = ! empty( $_REQUEST['json'] ); // New-style request 
  3.  
  4. $post_ID = intval( $_POST['post_id'] ); 
  5. if ( ! current_user_can( 'edit_post', $post_ID ) ) 
  6. wp_die( -1 ); 
  7.  
  8. $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
  9.  
  10. if ( $json ) 
  11. check_ajax_referer( "update-post_$post_ID" ); 
  12. else 
  13. check_ajax_referer( "set_post_thumbnail-$post_ID" ); 
  14.  
  15. if ( $thumbnail_id == '-1' ) { 
  16. if ( delete_post_thumbnail( $post_ID ) ) { 
  17. $return = _wp_post_thumbnail_html( null, $post_ID ); 
  18. $json ? wp_send_json_success( $return ) : wp_die( $return ); 
  19. } else { 
  20. wp_die( 0 ); 
  21.  
  22. if ( set_post_thumbnail( $post_ID, $thumbnail_id ) ) { 
  23. $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID ); 
  24. $json ? wp_send_json_success( $return ) : wp_die( $return ); 
  25.  
  26. wp_die( 0 );