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 '[youtube http://www.youtube.com/watch?v=%27+.+urlencode%28+%24vars%5B%27v%27%5D+%29+.+%27&w=420&h=315]';
} 
add_shortcode( 'youtube', 'startfunction_convert_youtube_sc' );
?>

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

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

  1. 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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s