after-header

Auto-Embed Media in WordPress Text Widgets

I’ve used a trick for quite awhile that allows me to place shortcodes in the text widgets on my site.

WordPress 2.9 was released with the ability to auto-embed media, such as youtube videos, in posts by simply pasting a plain text URL into the post content.  I have thought for some time that it would be really great to be able to borrow from the idea behind the shortcode in text widget trick to enable the same auto-embed functionality in my text widgets.

With the help of a couple of my fellow StudioPress forum moderators, Charles Clarkson and Gary Jones, I now have a solution!  The YouTube video in the footer of this site, for example, is placed using the

PGNvZGU+W2VtYmVkXTwvY29kZT4=

shortcode to automatically embed the video.

This trick can be acomplished by adding the following code to the functions.php file in your sites active theme directory.

PGNvZGU+DQovL0VuYWJsZSBBdXRvRW1iZWRzIGZyb20gUGxhaW4gVGV4dCBVUkxzIGluIFRleHQgV2lkZ2V0cw0KYWRkX2ZpbHRlciggJ3dpZGdldF90ZXh0JywgYXJyYXkoICR3cF9lbWJlZCwgJ3J1bl9zaG9ydGNvZGUnICksIDggKTsNCmFkZF9maWx0ZXIoICd3aWRnZXRfdGV4dCcsIGFycmF5KCAkd3BfZW1iZWQsICdhdXRvZW1iZWQnKSwgOCApOw0KPC9jb2RlPg==

This should allow both plain text urls in text widgets and the

PGNvZGU+W2VtYmVkXTwvY29kZT4=

shortcode:

Examples:

PGNvZGU+aHR0cDovL3d3dy55b3V0dWJlLmNvbS93YXRjaD92PXltd2taQzVfaFk4PC9jb2RlPg==

Would look like this:

PGNvZGU+W2VtYmVkIHdpZHRoPSIyNTAiIGhlaWdodD0iMTY1Il1odHRwOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9eW13a1pDNV9oWThbL2VtYmVkXTwvY29kZT4=

Would look like this: