wp_oembed_add_provider()
Adds an oEmbed provider. This is a URL that will be parsed in the content to output HTML code from another site.
More about oEmbed in WordPress.
Only suitable for sites that support the oEmbed format. For others, to process the URL in the content, you need to create a handler using the function wp_embed_register_handler().
No Hooks.
Returns
null. Nothing.
Usage
wp_oembed_add_provider( $format, $provider, $regex );
- $format(string) (required)
- The URL pattern that will be searched in the content. Here you can specify a regex or use the
*, which will then be replaced with(.+). - $provider(string) (required)
The URL endpoint from which the site provides data in oEmbed format.
Set an empty value, then WP will try to get this link from the HTML headers of the processed link (discovery).
- $regex(true/false)
- Whether a regex is specified in the $format parameter. true - regex is passed.
Default: false
Examples
#1 Example of adding a provider
Suppose we need to add oEmbed embedding for website wizer.me and we found out that this website is an oEmbed provider and found such data to handle such request:
- URL scheme:
http://*.wizer.me/learn/* - URL scheme:
https://*.wizer.me/learn/* - URL scheme:
http://*.wizer.me/preview/* - URL scheme:
https://*.wizer.me/preview/* - API endpoint:
http://app.wizer.me/api/oembed.{format} - Supports discovery via <link> tags
// Add an embedding for wizer.me
add_action( 'init', 'wizerme_oembed_provider' );
function wizerme_oembed_provider() {
wp_oembed_add_provider( 'http://*.wizer.me/learn/*', 'http://app.wizer.me/api/oembed.{format}', false );
wp_oembed_add_provider( 'https://*.wizer.me/learn/*', 'http://app.wizer.me/api/oembed.{format}', false );
wp_oembed_add_provider( 'http://*.wizer.me/preview/*', 'http://app.wizer.me/api/oembed.{format}', false );
wp_oembed_add_provider( 'https://*.wizer.me/preview/*', 'http://app.wizer.me/api/oembed.{format}', false );
}
Now when you insert a link like https://app.wizer.me/preview/1J09IV into the content, it will be processed and embedded in the content as an iframe.
#2 Example of adding a provider
The format here specifies a regular expression.
!!! This is just a demonstration, in fact, youtube provider is already registered in WordPress.
add_action( 'init', 'youtube_oembed_provider' );
function youtube_oembed_provider() {
wp_oembed_add_provider( '#https?://youtu\.be/.*#i', 'https://www.youtube.com/oembed', true );
} #3 Examples of adding providers from the kernel
See code of the WP_oEmbed::__construct() method.
Notes
- See: WP_oEmbed
Changelog
| Since 2.9.0 | Introduced. |
wp_oembed_add_provider() wp oembed add provider code WP 6.9
function wp_oembed_add_provider( $format, $provider, $regex = false ) {
if ( did_action( 'plugins_loaded' ) ) {
$oembed = _wp_oembed_get_object();
$oembed->providers[ $format ] = array( $provider, $regex );
} else {
WP_oEmbed::_add_provider_early( $format, $provider, $regex );
}
}