wp_ajax_send_link_to_editor

Ajax handler for sending a link to the editor.

Description

wp_ajax_send_link_to_editor(); 

Generates the HTML to send a non-image embed link to the editor.

Backward compatible with the following filters: - file_send_to_editor_url - audio_send_to_editor_url - video_send_to_editor_url


Usage

  1. if ( !function_exists( 'wp_ajax_send_link_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_link_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_link_to_editor() { 
  2. global $post, $wp_embed; 
  3.  
  4. check_ajax_referer( 'media-send-to-editor', 'nonce' ); 
  5.  
  6. if ( ! $src = wp_unslash( $_POST['src'] ) ) 
  7.  
  8. if ( ! strpos( $src, '://' ) ) 
  9. $src = 'http://' . $src; 
  10.  
  11. if ( ! $src = esc_url_raw( $src ) ) 
  12.  
  13. if ( ! $link_text = trim( wp_unslash( $_POST['link_text'] ) ) ) 
  14. $link_text = wp_basename( $src ); 
  15.  
  16. $post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 ); 
  17.  
  18. // Ping WordPress for an embed. 
  19. $check_embed = $wp_embed->run_shortcode( '[embed]'. $src .'[/embed]' ); 
  20.  
  21. // Fallback that WordPress creates when no oEmbed was found. 
  22. $fallback = $wp_embed->maybe_make_link( $src ); 
  23.  
  24. if ( $check_embed !== $fallback ) { 
  25. // TinyMCE view for [embed] will parse this 
  26. $html = '[embed]' . $src . '[/embed]'; 
  27. } elseif ( $link_text ) { 
  28. $html = '<a href="' . esc_url( $src ) . '">' . $link_text . '</a>'; 
  29. } else { 
  30. $html = ''; 
  31.  
  32. // Figure out what filter to run: 
  33. $type = 'file'; 
  34. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) ) 
  35. && ( 'audio' == $ext_type || 'video' == $ext_type ) ) 
  36. $type = $ext_type; 
  37.  
  38. /** This filter is documented in wp-admin/includes/media.php */ 
  39. $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $link_text ); 
  40.