How to create a shortcode for video embed in WordPress?

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

Shortcodes in WordPress are codes that allow you to do various things with little effort. The reason to introduce them was to allow the administrator to execute code inside WordPress posts, pages, and widgets without writing any code directly.


See code snippet that is used to create a shortcode to embed a video.
Shortcode with the name ‘video_embed’ will be created and admin can reuse anywhere in posts, pages and widgets.

To use this shortcode you need to pass three attributes along with the shortcode name

[video_embed src='URL' width='WIDTH OF VIDEO' height='HEIGHT OF VIDEO']

You need to put below code snippet in your theme’s functions.php file.

// Add Shortcode
function video_embed_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'src' => '',
            'width' => '',
            'height' => '',
        ),
        $atts,
        'video_embed'
    );

    // Return custom embed code
    return '<embed 
             src="' . $atts['src'] . '"
             width="' . $atts['width'] . '"
             height="' . $atts['height'] . '"
             type="application/x-shockwave-flash"
             allowscriptaccess="always"
             allowfullscreen="true">';

}
add_shortcode( 'video_embed', 'video_embed_shortcode' );

Leave a Comment