I think there's some weirdness with [embed]
shortcodes where you have to run them through 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: however as no providers are registered this won't work as you noticed(!), so I think the easiest thing to do is to call wp_oembed_get()
direct:
$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 );
You may want to use transients to avoid repeated calls to the provider - here's a version of stuff I use:
// 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 );
}
Then set the global in your ajax callback, eg
global $wpse175427_oembed_transient_expiration;
$wpse175427_oembed_transient_expiration = DAY_IN_SECONDS;
and call wpse175427_oembed_get()
instead of wp_oembed_get()
.
$post
and$wp_embed
objects in my ajax callback, and then in my content I addeddo_shortcode( $wp_embed->autoembed( get_the_content( '' ) ) )
. This did the trick. Thanks for the pointers :) – dingo_d Nov 28 '16 at 8:45