Last active
August 29, 2015 14:04
-
-
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 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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