BuddyCamp Minneapolis begins!
BuddyCamp begins! #wcmpls twitter.com/alisothegeek/s…
— Alison Barrett (@alisothegeek) April 28, 2013
I’m Alison Barrett, and I like WordPress. A lot. I’m a Code Wrangler at Automattic, the company behind WordPress.com. You might want to read a little more about me, check out some of my WordPress tutorials, or drop me a line. Or just do some clicks on the hyperspot links on the Web Page below.
BuddyCamp begins! #wcmpls twitter.com/alisothegeek/s…
— Alison Barrett (@alisothegeek) April 28, 2013
In the past week, I’ve added a couple of oEmbed handlers to my WordPress theme to enable easy embedding of external media, specifically SpeakerDeck (example) and Meetup.com (example). It’s really easy, and I figured I’d share the knowledge for those who want it.
oEmbed is a standardized format to allow for easy embedding of external content in a website. A service that supports oEmbed functionality will have an “endpoint URL” that all requests point to. Usually, you just pass the URL of the item you want to embed to that endpoint, and it returns the HTML required to embed it. This is what allows you to embed YouTube videos in WordPress by putting the URL on its own line in the editor.
We need to hook into an action in WordPress to make sure we’re adding the oEmbed handler at the right time, and then use a built-in WordPress function to register a new URL pattern to look for. In this case, the init hook will do the trick.
In your theme’s functions.php file, add the following code (replacing ag with your own function prefix if desired):
add_action( 'init', 'ag_add_oembed_handlers' );
function ag_add_oembed_handlers() {
wp_oembed_add_provider( '#https?://(www\.)?speakerdeck.com/.*#i', 'https://speakerdeck.com/oembed.json', true );
}
Now if you put a SpeakerDeck URL on its own line in the WordPress editor, you’ll have an embedded SpeakerDeck presentation!
The wp_oembed_add_provider function accepts three parameters:
The first parameter can be either a wildcard string (using an asterisk * for the wildcard) or it can be a full-blown regex pattern. I prefer regex, because I’m nerdy and that’s what I’m supposed to prefer. Let’s look at the regex pattern I used:
#https?://(www\.)?speakerdeck.com/.*#i
We’ll work from the outside in. # is marking the beginning and end of the pattern. The i at the very end is telling it to use a case insensitive search.
A ? means that the item immediately preceding it is optional. It’s used twice here: for the “s” in “https” and the “www.” before the domain. Since the “s” and “www.” are optional, all of the following URLs would match this pattern:
https://speakerdeck.com/aliso/advanced-actions-and-filters http://speakerdeck.com/aliso/advanced-actions-and-filters https://www.speakerdeck.com/aliso/advanced-actions-and-filters http://www.speakerdeck.com/aliso/advanced-actions-and-filters
At the end of the pattern is .*. The period will match any character except for a line break character (\r or \n). The asterisk is similar to the question mark in that it applies to the item immediately preceding it. It means “0 or more of the previous item.” This accounts for the rest of the URL after speakerdeck.com/.
If a service supports oEmbed, you can usually find the oEmbed endpoint in the developer or API documentation for that service. When in doubt, use Google: “_______ oembed endpoint” usually gets you the information you need.
When this is set to true, the function treats the first parameter as a regex pattern. The other option is to use a simple wildcard string, which would look something like this:
https://speakerdeck.com/*
It’s not as powerful or flexible as regex, but it still gets the job done.
The code for adding oEmbed support for Meetup.com looks very similar to the code for SpeakerDeck. I can use the same function and just add another line to it.
add_action( 'init', 'ag_add_oembed_handlers' );
function ag_add_oembed_handlers() {
wp_oembed_add_provider( '#https?://(www\.)?speakerdeck.com/.*#i', 'https://speakerdeck.com/oembed.json', true );
wp_oembed_add_provider( '#https?://(www\.)?meetup.com/.*#i', 'http://api.meetup.com/oembed', true );
}
That’s it! Once I added that to my theme, I could embed SpeakerDeck presentations and Meetup events. Now go add oEmbed handlers to your own site! Have fun!
I’ll be giving my “Advanced Actions & Filters” talk at the next MSP WordPress meetup.
I spoke at WordCamp Phoenix this weekend! I led two sessions:
Customize the WordPress login page and admin Dashboard to be more client-friendly with just a few lines of code in your theme.
Demo files: http://aliso.me/fridayfiles
Learn how actions & filters work in WordPress core, discover more about the template_redirect action, and learn how (and why) to make your own hooks.
Here are some photos from my Advanced Actions & Filters presentation:
(Full image gallery for WordCamp Phoenix 2013, courtesy of Chris Frailey Photography: http://aliso.me/5)
Bash Crash Course = Brash Course? Hm.
I find myself installing WordPress on my local development environment a lot. Often enough that the “famous 5 minute install” feels 4.5 minutes too long.
A while ago I worked on a bash script (command line) that created a wizard of sorts to automate creating new WordPress installations. It was meant for use setting up installations for client sites, which needed more customization than the average local installation does. It took too long.
I updated the code to take away the customization in favor of speed and efficiency, and voilà! A 20-second setup for WordPress development sites.
This tutorial is split up over three posts:
For an embarrassingly long time, my Custom Styles Dropdown plugin has been breaking WordPress installations. I offer my apologies, and an updated plugin:
Download Custom Editor Styles 1.1
I have also updated the TinyMCE Styles Dropdown tutorial with working code. Allons-y!