Support » Fixing WordPress » YouTube video embededd in a new blog page

  • Hi,
    I searched a lot and now I’m wondering if it’s possibile:
    I have a page with a list of 10 youtube videos.

    I’d like to open the video embededd in a new wordpress page (so video will show embededd in a page with my theme).
    Problem is that I change that video often, I can’t create/modify every single page containing video.

    Maybe I need a way to create a “template” page with code to show an embededd video, but to pass the video id by parameter to that page.

    you think it’s possible ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • It is definitely possible. Create your video display page and insert the shortcode [my-yt] where you want the video to show.

    Put this function in functions.php:

    function my_youtube_embed($atts) {
       // Display a YouTube video if passed the 'myv' parameter in the URL with the value of a YouTube video id.
       $txt = '';
       if ($_GET['myv']) {
          $video = $_GET['myv'];
          $txt = '<span class="youtube"><iframe title="YouTube video player" class="youtube-player" type="text/html" width="425" height="344" src="http://www.youtube.com/embed/' . $video . '?wmode=transparent&fs=1&hl=en&modestbranding=1&iv_load_policy=3&showsearch=0&rel=1&theme=dark" frameborder="0" allowfullscreen></iframe></span>';
       }
       return $txt;
    }
    add_shortcode('my-yt','my_youtube_embed');

    Call the display page by including the ‘myv=video-id’, replacing ‘video-id’ with the id of a video to show.

    Note you may need to modify the string used to view the video to suit your site.

    You can test this with this link:

    http://mcdstaging.info/?page_id=37&myv=HiqKBh3EFns

    Try out your own video id also. Note that there is no error checking done, so you might want to add that.

    As an alternative you can use this function:

    function my_video_filter($content) {
       // Substitute youtube_sc shortcode for my_video shortcode so Youtube Shortcode plugin will get it
       $video = ($_GET['myv']) ? $_GET['myv'] : false;
       if ($video) {
          $video = strip_tags($video);
          if (preg_match('/\[my_video([ \]])/',$content,$matches)) {
             return preg_replace('/\[my_video([ \]])/',"[youtube_sc url=$video $matches[1]",$content);
          } else {
             return $content . '<p>NO [my_video FOUND</p>';
          }
       } else {
          return $content . '<p>NO MYV FOUND</p>';
       }
    }
    add_filter('the_content','my_video_filter');

    with the Youtube Shortcode plugin and a shortcode of [my_video].

    The advantage of this method is that you can include Youtube Shortcode parameters in the my_video shortcode. For example, you can set the width and height of the video, and set it to autoplay, like this:

    [my_video width='400' height='280' autoplay=1]

    The valid parameters are documented here.

    Many thanks for your help!

    IMPORTANT UPDATE! I put in some code to help me debug the function and neglected to remove it before I posted the function. The corrected function should be this:

    function my_video_filter($content) {
       // Substitute youtube_sc shortcode for my_video shortcode so Youtube Shortcode plugin will get it
       $video = ($_GET['myv']) ? $_GET['myv'] : false;
       if ($video) {
          $video = strip_tags($video);
          if (preg_match('/\[my_video([ \]])/',$content,$matches)) {
             return preg_replace('/\[my_video([ \]])/',"[youtube_sc url=$video $matches[1]",$content);
          }
       }
       return $content;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘YouTube video embededd in a new blog page’ is closed to new replies.