wp_ajax_send_link_to_editor

函式
wp_ajax_send_link_to_editor ( No parameters )

wp_ajax_send_link_to_editor:此函式處理ajax請求,以向post編輯器傳送連結。它接受連結URL和標題作為引數,並將連結作為HTML響應返回。

傳送連結到編輯器的Ajax處理程式。

生成HTML來傳送一個非圖片的嵌入連結到編輯器。

向後相容以下過濾器:
– file_send_to_editor_url
– audio_send_to_editor_url
– video_send_to_editor_url

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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="https://www.wbolt.com/tw/go?_=ae8c59f1c2aHR0cDovLyYjMDM5OyUyMC4lMjAkc3JjJTIwLiUyMCYjMDM5Ow%3D%3D" rel="noopener noreferrer nofollow" target="_blank">' . $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="https://www.wbolt.com/tw/go?_=95214d389baHR0cDovL3dpbGwlMjBwYXJzZSUyMHRoaXMuJGh0bWwlMjA9JTIwJiMwMzk7JTVCZW1iZWQlNUQmIzAzOTslMjAuJTIwJHNyYyUyMC4lMjAmIzAzOTs%3D" rel="noopener noreferrer nofollow" target="_blank"> 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 );
}
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="https://www.wbolt.com/tw/go?_=ae8c59f1c2aHR0cDovLyYjMDM5OyUyMC4lMjAkc3JjJTIwLiUyMCYjMDM5Ow%3D%3D" rel="noopener noreferrer nofollow" target="_blank">' . $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="https://www.wbolt.com/tw/go?_=95214d389baHR0cDovL3dpbGwlMjBwYXJzZSUyMHRoaXMuJGh0bWwlMjA9JTIwJiMwMzk7JTVCZW1iZWQlNUQmIzAzOTslMjAuJTIwJHNyYyUyMC4lMjAmIzAzOTs%3D" rel="noopener noreferrer nofollow" target="_blank"> 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 ); }
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 = '<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 );
}

常見問題

FAQs
檢視更多 >