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:

Shortcodes in the Sidebar

Shortcodes are used to insert complicated or customized content into a page or post with minimal fuss. They usually look something like this:

[shortcodekeyword]
Sometimes they will have other information along with them like this:

[shortcodekeyword value='somevalue']

Since WordPress 2.5 the process of outputting shortcode has been simplified. Unfortunately it’s not posssible, out of the box, to use shortcodes in the sidebar text widgets. Enabling this feature is simple, though. Simply add the following line to the functions.php file in your active theme:

PGNvZGU+YWRkX2ZpbHRlcignd2lkZ2V0X3RleHQnLCAnZG9fc2hvcnRjb2RlJyk7PC9jb2RlPg==

Of course, if the thought of adding code to your theme gives you hives you can always use this plugin: Sidebar Shortcodes