Skip to content

Instantly share code, notes, and snippets.

@JayWood
Forked from jtsternberg/get_oembed.php
Created July 29, 2014 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayWood/74ac2a2138031f7c2bf5 to your computer and use it in GitHub Desktop.
Save JayWood/74ac2a2138031f7c2bf5 to your computer and use it in GitHub Desktop.
<?php
function get_oembed( $url, $post_id, $args = array() ) {
global $wp_embed;
$oembed_url = esc_url( $url );
// Sanitize object_id
$post_id = absint( $post_id );
$args = wp_parse_args( $args, array(
'oembed_args' => array(),
'fallback_to_url' => false,
) );
$wp_embed->post_ID = $post_id;
$embed_args = '';
foreach ( $args['oembed_args'] as $key => $val ) {
$embed_args .= ' '. $key .'="'. $val .'"';
}
// ping WordPress for an embed
$check_embed = $wp_embed->run_shortcode( '[embed'. $embed_args .']'. $oembed_url .'[/embed]' );
// fallback that WordPress creates when no oEmbed was found
$fallback = $wp_embed->maybe_make_link( $oembed_url );
if ( $check_embed == $fallback ) {
return $args['fallback_to_url'] ? $fallback : false;
}
return $check_embed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment