wp_ajax_send_link_to_editor()
云策文档标注
概述
wp_ajax_send_link_to_editor() 是一个 WordPress AJAX 处理函数,用于生成非图片嵌入链接的 HTML 并发送到编辑器。它支持文件、音频和视频类型的链接,并向后兼容相关过滤器。
关键要点
- 处理 AJAX 请求,将链接发送到编辑器,生成 HTML 输出
- 支持文件、音频和视频类型,通过扩展名自动识别类型
- 使用 WP_Embed 类检查 oEmbed 支持,并生成回退链接
- 应用过滤器 file_send_to_editor_url、audio_send_to_editor_url、video_send_to_editor_url 进行自定义
- 包含安全检查,如 check_ajax_referer() 和 sanitize_url()
代码示例
function wp_ajax_send_link_to_editor() {
global $post, $wp_embed;
check_ajax_referer( 'media-send-to-editor', 'nonce' );
$src = wp_unslash( $_POST['src'] );
if ( ! $src ) {
wp_send_json_error();
}
if ( ! strpos( $src, '://' ) ) {
$src = 'http://' . $src;
}
$src = sanitize_url( $src );
if ( ! $src ) {
wp_send_json_error();
}
$link_text = trim( wp_unslash( $_POST['link_text'] ) );
if ( ! $link_text ) {
$link_text = wp_basename( $src );
}
$post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 );
// Ping WordPress for an embed.
$check_embed = $wp_embed->run_shortcode( '' . $src . '' );
// Fallback that WordPress creates when no oEmbed was found.
$fallback = $wp_embed->maybe_make_link( $src );
if ( $check_embed !== $fallback ) {
// TinyMCE view for will parse this.
$html = '' . $src . '';
} elseif ( $link_text ) {
$html = '' . $link_text . '';
} else {
$html = '';
}
// Figure out what filter to run:
$type = 'file';
$ext = preg_replace( '/^.+?.([^.]+)$/', '$1', $src );
if ( $ext ) {
$ext_type = wp_ext2type( $ext );
if ( 'audio' === $ext_type || 'video' === $ext_type ) {
$type = $ext_type;
}
}
/** This filter is documented in wp-admin/includes/media.php */
$html = apply_filters( "{$type}_send_to_editor_url", $html, $src, $link_text );
wp_send_json_success( $html );
}注意事项
- 函数依赖于 $_POST 参数,如 'src'、'link_text' 和 'post_id',需确保 AJAX 请求正确传递
- 使用 wp_ext2type() 根据文件扩展名判断类型,可能影响过滤器选择
- 安全措施包括 nonce 验证和 URL 清理,开发者应遵循类似实践
原文内容
Handles sending a link to the editor via AJAX.
Description
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
Source
function wp_ajax_send_link_to_editor() {
global $post, $wp_embed;
check_ajax_referer( 'media-send-to-editor', 'nonce' );
$src = wp_unslash( $_POST['src'] );
if ( ! $src ) {
wp_send_json_error();
}
if ( ! strpos( $src, '://' ) ) {
$src = 'http://' . $src;
}
$src = sanitize_url( $src );
if ( ! $src ) {
wp_send_json_error();
}
$link_text = trim( wp_unslash( $_POST['link_text'] ) );
if ( ! $link_text ) {
$link_text = wp_basename( $src );
}
$post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 );
// Ping WordPress for an embed.
$check_embed = $wp_embed->run_shortcode( '<a href="http://'%20.%20$src%20.%20'">' . $src . '</a>' );
// Fallback that WordPress creates when no oEmbed was found.
$fallback = $wp_embed->maybe_make_link( $src );
if ( $check_embed !== $fallback ) {
// TinyMCE view for <a href="http://will%20parse%20this.$html%20=%20'%5Bembed%5D'%20.%20$src%20.%20'"> will parse this.
$html = '' . $src . '</a>';
} elseif ( $link_text ) {
$html = '<a href="' . esc_url( $src ) . '">' . $link_text . '</a>';
} else {
$html = '';
}
// Figure out what filter to run:
$type = 'file';
$ext = preg_replace( '/^.+?.([^.]+)$/', '$1', $src );
if ( $ext ) {
$ext_type = wp_ext2type( $ext );
if ( 'audio' === $ext_type || 'video' === $ext_type ) {
$type = $ext_type;
}
}
/** This filter is documented in wp-admin/includes/media.php */
$html = apply_filters( "{$type}_send_to_editor_url", $html, $src, $link_text );
wp_send_json_success( $html );
}
Hooks
- apply_filters( “{$type}_send_to_editor_url”, string $html, string $src, string $title )
-
Filters the URL sent to the editor for a specific media type.
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Introduced. |