我觉得有点奇怪[embed]
必须运行它们的短代码WP_Embed::run_shortcode()
, eg公司
if ( strpos( $my_content, \'[embed]\' ) !== false ) {
global $wp_embed;
$my_content = $wp_embed->run_shortcode( $my_content );
}
$my_content = do_shortcode( $my_content );
Update: 但是,由于没有注册提供商,这将无法如您所注意到的那样(!),所以我认为最简单的方法就是打电话
wp_oembed_get()
直接:
$args = array();
global $content_width;
$args[\'width\'] = $content_width ? $content_width : 300;
// Simplistic parse - look for a url on its own line.
$lines = explode( "\\n", $my_content );
foreach ( $lines as $i => $line ) {
$line = trim( $line );
if ( stripos( $line, \'http\' ) === 0 ) {
if ( $oembed_html = wp_oembed_get( $line, $args ) ) {
$lines[$i] = $oembed_html;
}
}
}
$my_content = implode( "\\n", $lines );
您可能希望使用瞬态来避免重复调用提供者-以下是我使用的东西的一个版本:
// Wrapper around wp_oembed_get() to use transients.
function wpse175427_oembed_get( $url, $args ) {
global $wpse175427_oembed_transient_expiration;
if ( $wpse175427_oembed_transient_expiration ) {
$transient_key = md5( $url . \'_\' . wpse175427_reduce_array( $args ) );
if ( $oembed_html = get_transient( $transient_key ) ) {
return $oembed_html;
}
}
//add_filter( \'oembed_fetch_url\', \'wpse175427_oembed_fetch_url\', 10, 3 );
//add_filter( \'oembed_result\', \'wpse175427_oembed_result\', 10, 3 );
if ( $oembed_html = wp_oembed_get( $url, $args ) ) {
if ( $wpse175427_oembed_transient_expiration ) {
set_transient( $transient_key, $oembed_html, $wpse175427_oembed_transient_expiration );
}
return $oembed_html;
}
return \'\';
}
// Helper to stringify args array (for transient key).
function wpse175427_reduce_array( $arr ) {
$ret = array();
foreach ( $arr as $key => $val ) {
$ret[] = \'`\' . $key . \'`=`\' . $val . \'`\';
}
return implode( \',\', $ret );
}
然后在ajax回调中设置全局值,例如
global $wpse175427_oembed_transient_expiration;
$wpse175427_oembed_transient_expiration = DAY_IN_SECONDS;
和电话
wpse175427_oembed_get()
而不是
wp_oembed_get()
.