Site icon StartFunction

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.

Exit mobile version