wp_ajax_send_attachment_to_editor

Ajax handler for sending an attachment to the editor.

Description

wp_ajax_send_attachment_to_editor(); 

Generates the HTML to send an attachment to the editor. Backward compatible with the filter and the chain of filters that follow.


Usage

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

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_send_attachment_to_editor() { 
  2. check_ajax_referer( 'media-send-to-editor', 'nonce' ); 
  3.  
  4. $attachment = wp_unslash( $_POST['attachment'] ); 
  5.  
  6. $id = intval( $attachment['id'] ); 
  7.  
  8. if ( ! $post = get_post( $id ) ) 
  9.  
  10. if ( 'attachment' != $post->post_type ) 
  11.  
  12. if ( current_user_can( 'edit_post', $id ) ) { 
  13. // If this attachment is unattached, attach it. Primarily a back compat thing. 
  14. if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) { 
  15. wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) ); 
  16.  
  17. $url = empty( $attachment['url'] ) ? '' : $attachment['url']; 
  18. $rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url ); 
  19.  
  20.  
  21. if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { 
  22. $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none'; 
  23. $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; 
  24. $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : ''; 
  25.  
  26. // No whitespace-only captions. 
  27. $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; 
  28. if ( '' === trim( $caption ) ) { 
  29. $caption = ''; 
  30.  
  31. $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. 
  32. $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ); 
  33. } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) { 
  34. $html = stripslashes_deep( $_POST['html'] ); 
  35. } else { 
  36. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : ''; 
  37. $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized 
  38.  
  39. if ( ! empty( $url ) ) { 
  40. $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>'; 
  41.  
  42. /** This filter is documented in wp-admin/includes/media.php */ 
  43. $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment ); 
  44.