Jhon Doe Photo

MARKO JAKIĆ

software engineer / web developer

Download vCard
  • How to programmatically embed YouTube video using iFrame

    Posted by markzero on Monday, December 12th, 2011

    First of all: Be sure to put the following code INSIDE THE LOOP! YouTube videos can be embedded into your posts and/or pages using custom fields. This way (or similar, like meta boxes) is good for your clients so they can put their own stuff to a site.

    Something similar to this:

    YouTube video code in custom field

    You can decide whether to put just the ID of a YouTube video, or the whole thing… Next we put this code into While loop:

     
    <?php $vid1=get_post_meta($post->ID, "video_url_id_1", true); ?>

    And now you put an iFrame:

     
    <iframe id="med-vid-1" width="640" height="355" src="http://www.youtube.com/embed/<?php echo $vid1; ?>?&amp;rel=0" frameborder="0" allowfullscreen></iframe>

    Be sure to encode HTML characters like & => &amp;

    Also, be sure to use /embed URL. IFrames are better than objects, because they are HTML5 ready. Downside is, they are not (yet) as strong with YouTube Javascript API as objects.

    Again: DON’T FORGET THE LOOOOP! ;) (if (have_posts()) : while (have_posts()) : the_post(); … endwhile; endif;)

    Tweet

    Posted in: PHP, Wordpress, YouTube.

    Share this story:

    Share 'How to programmatically embed YouTube video using iFrame' on Bebo Share 'How to programmatically embed YouTube video using iFrame' on Blogger Share 'How to programmatically embed YouTube video using iFrame' on Delicious Share 'How to programmatically embed YouTube video using iFrame' on Digg Share 'How to programmatically embed YouTube video using iFrame' on Fark Share 'How to programmatically embed YouTube video using iFrame' on Google Bookmarks Share 'How to programmatically embed YouTube video using iFrame' on LinkedIn Share 'How to programmatically embed YouTube video using iFrame' on Mixx Share 'How to programmatically embed YouTube video using iFrame' on Newsvine Share 'How to programmatically embed YouTube video using iFrame' on reddit Share 'How to programmatically embed YouTube video using iFrame' on Spurl Share 'How to programmatically embed YouTube video using iFrame' on Squidoo Share 'How to programmatically embed YouTube video using iFrame' on StumbleUpon Share 'How to programmatically embed YouTube video using iFrame' on Yahoo! Buzz Share 'How to programmatically embed YouTube video using iFrame' on Email

    Leave a Reply

    Click here to cancel reply.

    • Useful Links

      • Image Resizer Online Tool
      • Image Rotator Online Tool
      • MJ Posts Extras
      • MJ Quick Comments
      • MJSlider
      • Random Text Paste
    • Categories

      • CSS
      • DOM
      • Javascript
      • JQuery
      • Linux
      • Magento
      • MySQL
      • PEAR
      • PHP
      • PHPUnit
      • Prototype JS
      • Ruby
      • SQL
      • Tech
      • Ubuntu
      • Wordpress
      • XML
      • YouTube

    Archives

    • July 2012
    • June 2012
    • April 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
  • HOME