wp_ajax_set_attachment_thumbnail

Ajax handler for setting the featured image for an attachment.

Description

wp_ajax_set_attachment_thumbnail(); 

Usage

  1. if ( !function_exists( 'wp_ajax_set_attachment_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_attachment_thumbnail(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_set_attachment_thumbnail() { 
  2. if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) { 
  3.  
  4. $thumbnail_id = (int) $_POST['thumbnail_id']; 
  5. if ( empty( $thumbnail_id ) ) { 
  6.  
  7. $post_ids = array(); 
  8. // For each URL, try to find its corresponding post ID. 
  9. foreach ( $_POST['urls'] as $url ) { 
  10. $post_id = attachment_url_to_postid( $url ); 
  11. if ( ! empty( $post_id ) ) { 
  12. $post_ids[] = $post_id; 
  13.  
  14. if ( empty( $post_ids ) ) { 
  15.  
  16. $success = 0; 
  17. // For each found attachment, set its thumbnail. 
  18. foreach ( $post_ids as $post_id ) { 
  19. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  20. continue; 
  21.  
  22. if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) { 
  23. $success++; 
  24.  
  25. if ( 0 === $success ) { 
  26. } else { 
  27.