NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate
Web Hosting Canada

Last modified : Sep 15 2022

Estimated reading time : 1 minute, 36 seconds - 151 words

Shortcode are an easy to integrate content for WordPress users. Here’s a Youtube shortcode for WordPress.

Here’s our Via Youtube shortcode. Simpy copy paste the code below inside your functions.php file.

add_shortcode( 'via_youtube', 'via_you_tube_shortcode' );
// Nous creons la fonction 
function via_you_tube_shortcode($atts) {
 $atts = shortcode_atts( array(
   'id'                 => '',
   'width'              => $width,
   'height'             => $height,
   ), $atts ); // Nous stockons les arguments 
   extract($atts); // Extraction des arguments ci dessus
 
   // Nous retournons les resultats en iframe avec la hauteur et la largeur et l'identifiant de la video qui sera present dans votre shortcode
   return'<iframe src="https://www.youtube.com/embed/'.$id.'" width="'.$width.'" height="'.$height.'" frameborder="0" allowfullscreen="allowfullscreen"></iframe>'; 
}

Now go into the desired page and insert the shortcode below:

[via_youtube id="BJ-CmHZrKHU" width="560" height="315"]

Grab your video’s ID in the URL of your browser and use it to replace the ID used in the shortcode.

Youtube uses the following resolutions:
560 * 315
640 * 360
853 * 480
1280 * 720

Also can be used with custom resolutions. Simply adjust the height and width parameter accordingly in the shortcode.

With a few click, you’ll have your Youtube video displayed on any page.
Here’s another solution so your shortcode is displayed in your WordPress widget.
Add the following line of code in your function.php file; make sure the code isn’t already there:

// Filtre permettant shortcodes visuels dans les widgets texte
add_filter('widget_text', 'do_shortcode');

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>