Skip to content

Instantly share code, notes, and snippets.

@pfefferle
Created September 8, 2015 08:45
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 pfefferle/62f7154c0ef2f94342fa to your computer and use it in GitHub Desktop.
Save pfefferle/62f7154c0ef2f94342fa to your computer and use it in GitHub Desktop.
/**
* Get the oEmbed endpoint URL for a given permalink.
*
* Pass an empty string as the first argument
* to get the endpoint base URL.
*
* @param string $permalink The permalink used for the `url` query arg.
* @param string|bool $format The requested response format.
* Provide false to get the default format.
*
* @return string
*/
function get_oembed_endpoint_url( $permalink = '', $format = false ) {
$url = add_query_arg( array( 'oembed' => 'true' ), home_url( '/' ) );
if ( function_exists( 'rest_url' ) ) {
$url = rest_url( 'wp/v2/oembed' );
}
/** This filter is defined in classes/class-plugin.php */
$default_format = apply_filters( 'rest_oembed_default_format', 'json' );
if ( $format === $default_format ) {
$format = false;
}
if ( '' === $permalink ) {
$url = add_query_arg( array(
'url' => $permalink,
'format' => $format,
), $url );
}
/**
* Filter the oEmbed endpoint URL.
*
* @param string $url The URL to the oEmbed endpoint.
* @param string $permalink The permalink used for the `url` query arg.
* @param string $format The requested response format.
*
* @return string
*/
$url = apply_filters( 'rest_oembed_endpoint_url', $url, $permalink, $format );
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment