Register oEmbed Providers

Introducing oEmbed provider generator

GenerateWP is introducing yet another cool tool for WordPress developers. It’s a simple tool, and very easy to use. Meet our new oEmbed provider generator ! The tool helps you to create custom code to register new oEmbed providers.

What is oEmbed?

oEmbed is a format that allows a website to display embedded content from third party sites using nothing but the URL, without having to parse the resource embed code.

For example, when you want to show YouTube videos in WordPress you don’t need to parse the YouTube embed code, you just need to put the video URL on its own line in the editor. But, for WordPress to auto embed the video, you need to register YouTube as an oEmbed provider.

Register oEmbed providers

WordPress is supporting several providers out-of-the-box like YouTube, Vimeo, Flicker, TED and many others). Also, WordPress provides developers the tools to register new providers.

If the third party site has oEmbed support, use the wp_oembed_add_provider() function, if not consider using wp_embed_register_handler().

Usage

You can register new providers using this simple code:

function custom_oembed_provider() {
	wp_oembed_add_provider( $format, $provider, $regex );
}
add_action( 'init', 'custom_oembed_provider' );

Parameters

The wp_oembed_add_provider() function accepts three parameters:

  1. $format (string) – The URL structure that the oEmbed provider supports.
  2. $provider (string) – The endpoint URL to the oEmbed provider.
  3. $regex (boolean) – Whether the $format parameter is a regex (true) or not (false). Default value is false.

Simple example

function kickstarter_oembed_provider() {
	wp_oembed_add_provider(
		'http://www.kickstarter.com/projects/*',
		'http://www.kickstarter.com/services/oembed',
		false
	);
}
add_action( 'init', 'kickstarter_oembed_provider' );

When your provider has a simple URL structure, enter a URL with an asterisk * for the wildcard.

Regex example

With complex URL structures use regex pattern:

function playbuzz_oembed_provider() {
	wp_oembed_add_provider(
		'#https?://(www\.)?playbuzz.com/.*#i',
		'https://www.playbuzz.com/api/oembed/',
		true
	);
}
add_action( 'init', 'playbuzz_oembed_provider' );

Now, Let’s examine the regex (first parameter):

#https?://(www\.)?playbuzz.com/.*#i

It means that you can use:

  • http://playbuzz.com/*
  • https://playbuzz.com/*
  • http://www.playbuzz.com/*
  • https://www.playbuzz.com/*

The meaning of the regex parameters:

# – Designate the beginning and the end of the pattern.
? – The preceding character is optional. https? means that the pattern can be ‘http’ and ‘https’ and (www\.)? means that the pattern can be with ‘www’ and without.
.* – Any character after the domain.
i – The at the end means that the pattern is case insensitive.

Live examples

We can register more providers using the oEmbed.com provider list:

Recipes from ifttt.com:

function ifttt_oembed_provider() {
	wp_oembed_add_provider(
		'#https?://(www\.)?ifttt.com/recipes/.*#i',
		'http://www.ifttt.com/oembed/',
		true
	);
}
add_action( 'init', 'ifttt_oembed_provider' );

Polls from PollEverywhere.com:

function polleverywhere_oembed_provider() {
	wp_oembed_add_provider(
		'http://www.polleverywhere.com/polls/*',
		'http://www.polleverywhere.com/services/oembed/',
		false
	);
}
add_action( 'init', 'polleverywhere_oembed_provider' );

Artists from rdio.com:

function rdio_oembed_provider() {
	wp_oembed_add_provider(
		'#https?://(www\.)?rdio.com/artist/.*#i',
		'http://www.rdio.com/api/oembed/',
		true
	);
}
add_action( 'init', 'rdio_oembed_provider' );

Playlists from official.fm:

function officialfm_oembed_provider() {
	wp_oembed_add_provider(
		'#https?://(www\.)?official.fm/playlists/.*#i',
		'http://official.fm/services/oembed.json',
		true
	);
}
add_action( 'init', 'officialfm_oembed_provider' );

Diagrams from cacoo.com:

function cacoo_oembed_provider() {
	wp_oembed_add_provider(
		'#https?://(www\.)?cacoo.com/diagrams/.*#i',
		'http://cacoo.com/oembed.json',
		true
	);
}
add_action( 'init', 'cacoo_oembed_provider' );

It’s very simple once you understand the concept.

Now you can go and register your own oEmbed providers using our new tool. Just make sure to save it as a public snippet so other members can enjoy your work, clone the code and use it in there projects!

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on email
Rami Yushuvaev

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups and hi-tech companies, providing development services.

Leave a Reply

Subscribe to our Blog

Receive email notifications when new blog posts are published.