Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtsternberg/26345676f270125822da to your computer and use it in GitHub Desktop.
Save jtsternberg/26345676f270125822da to your computer and use it in GitHub Desktop.
Get oembed for a url. EDIT: nevermind, use wp_oembed_get( $url, $args )
<?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