Convert WordPress.com youtube shortcode to WordPress.org

WordPress.com includes some unique features for their users, such as the ability to embed videos using the [youtube] shortcode. Trouble arises when users migrate their blogs to a self-hosted solution using WordPress.org, since the format is incompatible.
In this post we will see how to create a shortcode that allows to display the [youtube] shortcode in our self-hosted blogs.

There are a bunch of approaches to this, and there are some rather complicated ones introducing preg_replace using regular expressions to obtain the URL but this is a very lightweight one that serves this purpose:

<?php
/**
 * Convert youtube shortcode from WordPress.com format so it works in WordPress.org
 * @param array $atts Parameters
 * @return string Video embedded using iframe version
 */
function startfunction_convert_youtube_sc( $atts ) {
    parse_str( parse_url( substr( $atts[0], 1 ), PHP_URL_QUERY ), $vars );
    return '
'; } add_shortcode( 'youtube', 'startfunction_convert_youtube_sc' ); ?>

you can paste the code above in your functions.php file or similar.

Advertisements

2 thoughts on “Convert WordPress.com youtube shortcode to WordPress.org”

  1. You can also just use the Jetpack plugin, and get that feature back, along with most of the other .com specific shortcodes.

  2. That’s a certainly good advice. This is just if someone doesn’t want to add Jetpack but it’s a good advice nevertheless, there are a lot of good stuff in Jetpack.

Leave a Reply