One of my favorite functions of the WordPress editor is now the automatic embedding of video and other rich media by simply putting the URL on it’s own line. Really, it’s amazing!
Here is my rip of the WordPress code (found in the WP_Embed
and WP_oEmbed
classes) and assembled into a class named AutoEmbed
.
It supports the following services (according to the WordPress Codex):
- blip.tv
- DailyMotion
- Flickr (both videos and images)
- FunnyOrDie.com
- Hulu
- Qik
- Photobucket
- PollDaddy
- Revision3
- Scribd
- SlideShare
- SoundCloud
- SmugMug
- Viddler
- Vimeo
- YouTube (only public and “unlisted” videos and playlists – “private” videos will not embed)
- WordPress.tv (only VideoPress-type videos for the time being)
You can get the “gist” of it by looking below; the example is followed by the class definition.
speedt_ouch says
Hi.
Thanks for sharing.
I was looking for something like this to use on my codeigniter website i’m making.
speedt_ouch says
Hello again.
I hope you can help.
I use tinymce and when I copy and past a youtube link, tinymce places the string inside paragraphstags in my CMS, that is NOT wordpress
So when I check the page, the string is not converted because of the paragraphs tags.
If I edit mysql and delete the paragraphs tags and reload my that the video string in converted and embed correctly.
Can you please provide a fix for the code also consider paragraphs tags?
I checked wordpress and the code does get embeded if there is paragraphs tags also.
Thanks again.
Josh Hartman says
Replace the
parse
andautoembed_callback
functions with the following and I think you’ll get the result you desire:Side note, the WordPress editor strips the
<p/>
tags and uses new lines instead. To test yourself, wrap something in<p/>
tags in the Text view, then switch to Visual, and then back to Text; now your<p/>
tags are gone. When it goes to display post content it useswpautop()
to add<p/>
tags back in.speedt_ouch says
Hi.
Thank you very much for your reply!
After I posted my questions here I searched a little and found that I was not the first with the tag issues.
I found a suggested fix on stackoverflow:
http://stackoverflow.com/questions/12115895/preg-replace-callback-matching-urls-in-html-paragraphs
I changed your code with the suggested code from stackoverflow and got it working
But now that you sent a fix I guess I will stick to your code with your fix instead 🙂
shimpoul says
Hi,
When i put another link on the $content i don’t get them on the result ! like for images link, http://www.google.com for example and others …
Can you give a fix for this ?
And how i can embed this function to your code ?
To get all non embed link a href=””……/a and display images links ?
Thank you in advance.
Josh Hartman says
This is outside the topic of oEmbed but see this code example for a solution: Linkify with Image Embed
shimpoul says
Thank you so much for this helpful function !
There a way to combine it with your code ?
I made like this:
speedt_ouch says
Hi.
Once again I see your help if possible.
I was wondering what would be the best way to add a custom class to make the videos responsive?
I’m using boostrap for styling and I need to add two divs automatically for each video.
http://getbootstrap.com/components/#responsive-embed
I did some changes to the function fetch() and added some class=… code, but its never written in the html so obviously I’m not doing it right.
Thanks in advance!
Josh Hartman says
The
data2html()
function is where you can modify the output for video/rich or wrap the output with an HTML tag. Depending on what HTML is returned by oEmbed you could use astr_replace
orpreg_replace
to inject your class or other code. Hope that gives you a direction at least.speedt_ouch says
Thanks.
I will git it a try 🙂
Andreas says
Hi.
When i use a ordinary link, like http://www.google.com i get blank results.
How can i fix this?
Josh Hartman says
Near line 56, inside the
autoembed_callback
function update the$return = ...
code:That will make it return the URL even if it can’t oEmbed it.
I’ll also update the Gist to reflect this change.
Andreas says
Thanks!
I will check it out 🙂