Home Replace YouTube Links in Text with Embed Code

    Replace YouTube Links in Text with Embed Code


    This article is one that resided on Internoetics. We’ve migrated it here (in part) for the sake of preservation. Only minor modifications were made to the code.

    ■ ■ ■

    The code on this page was originally applied to our old (and soon-to-be reborn) Usenet website. The text within each Usenet post is rather static in nature so we applied as much formatting as we could think of to bring those newsgroup articles to life. Part of this strategy included identifying YouTube video links and making that video available from within content of the post.

    Consider this text:

    As part of an aviation media venture, we recorded various Boeing 777 test footage in different situations. We took several hours of video in a Boeing 777 simulator to determine the most effective means of communicating various principles. Take, for example, this video that illustrates part of the pre-takeoff safety brief. After purchasing a few GoPro cameras, our initial camera tests were conducted simply by attaching the unit to the quarter windows of an aircraft. Here is an approach into LAX , while this is an approach into Sydney (http://youtu.be/T4eZ24ZjRgE). The next stage of our 777 Video Series is due to commence early September.

    Note: None of the referenced videos above are live to public.

    Find YouTube Videos and Create an Array

    By parsing the text body you may extract each YouTube video reference (and create an array of all the discovered YouTube videos).

    The $output array will look like this:

    To remove duplicate array values we use array_unique($output);. We can now do whatever we want with the returned video values. The most common application might be rendering each video with embed code or displaying video details with a thumbnail.

    Render YouTube Videos from Array

    If all I wanted to do was render the embed code of each video we found under the text body, I could use the following:

    The above code will embed each video on the page as follows:

    YouTube Videos Embedded into Page

    YouTube Videos Embedded into Page

    While the above shows only a static image, it’s an actual video that’s rendered in a tower (not unlike the WP shortcode provided here).

    We have a post forthcoming on our blog that deals with retrieving video details from YouTube’s API. Using the API we can provide additional video details such as the title, description, running time, and interactions.

    Return Video Thumbnails

    A modified version of the function will return all discovered videos in a row of thumbnails. The function below is isn’t a solution; it’s just illustrative of how the idea might be implemented. We’ve used the second beliefmedia_youtube_thumbnail() function as provided on this page. The example function is as follows:

    The result:

    Replace YouTube Links in Text with Embed Code

    Again, it’s just an example; the point is that you can perform any manner of tricks from the resulting video array.

    Short URL for this post: http://shor.tt/2nIu