Posted by on Mar 31, 2016 in Development, Javascript, PHP | No Comments

Well, that title is a mouthful, let me explain. Recently I had the need to create a shortcode to embed YouTube videos, no problem right? Well, lets introduce a few more variables. First we want to fire custom Google Analytics events on video play, pause and complete which is easy enough to work into the shortcode. If we use the YouTube JavaScript API instead of iframe embed it’s very simple to fire custom events, there are plenty of examples out there. But in addition, the shortcode also needed to be able to be used numerous times on a page. Below is my solution, feel free to comment if you want to improve on or have questions about any of the code.

The solution assumes a shortcode likeĀ [youtube id='YouTubeVideoId' width='664' height='375'] where width and height are optional as I have defaults set.

Tagged: , , , , ,