To use plugins for embedding media can be troublesome. You can’t choose all available parameters and you end up with something that is ok but not good enough. Best way of embedding media is to use the websites own code. If we take youtube as an example there is tons of parameters that can be set, see our article about “How to Post Good Looking YouTube Videos“. If you are multiple authors on a website or if you post many videos it is inconvenient to remember or having to copy this code every time. The solutions is shortcodes. A shortcode is a php script that takes a few input parameters and returns the complete code for the embedded video. This is shortcode: [nyplockadyoutube id=d9CPNCjUhB0 width=440 height=240] for this:
<code>
<iframe class=”youtube-player” type=”text/html” width=”640″ height=”360″ src=”http://www.youtube.com/embed/d9CPNCjUhB0?autohide=1&showinfo=0&theme=light&color=white&
autoplay=0&controls=1&disablekb=0&hd=1&
iv_load_policy=3&rel=0″ frameborder=”0″>
</iframe>
</code>
To create a shortcode you have to put a function in one of your wordpressthemes .php files. Most common is to put it in already existing functions.php. An even better way is to create your own my_shortcodes.php and include it in functions.php with include(‘my_shortcodes.php’);.
The function it self looks like this:
//YouTube Shortcode
function NyplockadYouTube($atts) {
extract(shortcode_atts(array(
‘id’ => ’2kJ978Mm9d8′, //default to the right
‘width’ => ’640′, //default to the right
‘height’ => ’360′ //default to the right
), $atts));
return ‘<p><code><iframe class=”youtube-player” type=”text/html” width=”‘.$width.’” height=”‘.$height.’” src=”http://www.youtube.com/embed/’.$id.’?autohide=1&showinfo=0&theme=light&color=white&autoplay=0&controls=1
disablekb=0&hd=1&iv_load_policy=3&rel=0″ frameborder=”0″>
</iframe>
</code></p>’;
}
add_shortcode(‘nyplockadyoutube’, ‘NyplockadYouTube’);
The shortcode accepts the parameters [nyplockadyoutube id=d9CPNCjUhB0 width=440 height=240] but can easily be modified with more parameters. The id=d9CPNCjUhB0 is the important parameter which tells what video to show. The ID is found in the end of a youtube URL or between “=” and “&”. The other parameters is optional and will get default values from the shortcode function.
Example:
http://www.youtube.com/watch?v=d9CPNCjUhB0&feature=g-all…
All we have to write in our posts is now [nyplockadyoutube id=d9CPNCjUhB0]. Pretty neat, right? Happy coding!