Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. Any idea how to use the WordPress sidebar for streaming Youtube videos?

    It should update automatically with the latest video from my youtube channel and be automatically resized to fit a box of 300x250px.

    Here is how to do this for Youtube videos:
    ( demo in sidebar: http://themes.go41.de/?wptheme=arthemia )

    We are going to use a function built into WordPress 2.9 and later, the class simplepie.
    To load this class you could install a plugin (SimplePie Plugin for WordPress) or use my easier way to load SimplePie.
    We add a call to functions.php to enable SimplePie. Most themes have a file called functions.php, there we add just one line above the end ( just above ?> ) the following code:
    -

    /**
     * If SimplePie class hasn't been loaded before try to load it again.
     */
    if ( file_exists(ABSPATH . WPINC . '/class-simplepie.php') ) {
    				@require_once (ABSPATH . WPINC . '/class-simplepie.php');
    			} else {
    				die (__('Error in file: ' . __FILE__ . ' on line: ' . __LINE__ . '.<br />The WordPress file "class-simplepie.php" with class SimplePie could not be included.'));
    			}

    -
    As soon this code is in place, the class simplepie is available and we can use it.

    Now we open up sidebar.php.
    I am going to explain this for the theme Arthemia, but it is working in all themes having about 300px space in width.
    In Arthemia's sidebar.php we are going to replace the 300x250px placeholder image.
    This is loaded in this line:

    <img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/banners/square.jpg" alt="" width="300px" height="250px" />

    leaving this line in place first we paste the following code just below this line, before the next </div>

    This code will show the latest four videos of a user channel as an image, linked directly to the youtube page:
    -

    <!-- start 4 youtube images -->
    <?php	if (class_exists('SimplePie')) {
    $user_name = herthatv;  //put your username here with ';' behind !!!!!!!!!!!!!!!!!!!!!!!!!
    $feed = new SimplePie();
    $feed->set_feed_url("http://gdata.youtube.com/feeds/api/users/".$user_name."/uploads");
    $feed->enable_cache(false); //  disable caching
    $feed->set_timeout(5);
    $success = $feed->init();
    $feed->handle_content_type();
        $YT_PlayerPage = "http://www.youtube.com/user/".$user_name."#play/uploads/";
        $YT_VideoNumber = 0;
        $ShowMax = 3; // to show 4! images
        foreach ($feed->get_items() as $item)
        {
        if ($enclosure = $item->get_enclosure())
        {
        $YT_VideoID = substr(strstr($item->get_permalink(), 'v='), 2, 11);
        echo '<a href="' . $YT_PlayerPage . $YT_VideoNumber . "/" . $YT_VideoID . '"title="' . $item->get_title() . '"> <img src="' . $enclosure->get_thumbnail() . '" width="148" height="122" alt="' . $item->get_title() . '"/></a>';
        }
        if($YT_VideoNumber == $ShowMax) break;
        $YT_VideoNumber++;
        }
     } // end Simplepie ?>
    <!-- end 4 youtube images -->

    -
    That's it? No, you have to replace the user_name with your User Name, otherwise you will see the Hertha BSC clips.

    This was easy, but what's about a really embedded video?

    The following code will embed the latest video of your channel in your sidebar:
    -

    <!-- start youtube video -->
    <?php	if (class_exists('SimplePie')) {
    $user_name = herthatv; //put your username here with ';' behind !!!!!!!!!!!!!!!!!!!!!!!!!
    $feed = new SimplePie(); // setup the feed
    $feed->set_feed_url("http://gdata.youtube.com/feeds/api/users/".$user_name."/uploads");
    $feed->enable_cache(false); //  disable caching
    $feed->set_timeout(5);
    $success = $feed->init();
    $feed->handle_content_type();
    ?>
    <?php if ($success): foreach ($feed->get_items(0, 1) as $item):
    if ($item) { ?>
    <?php
    	$YT_VideoID = substr(strstr($item->get_permalink(), 'v='), 2, 11); //get just ID of latest clip
     ?>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2/swfobject.js"></script>
    <a name="ytplayer"></a>
    <div id="ytplayer_div">You need Flash player 8+ and JavaScript enabled to view this video.</div>
    <script type="text/javascript">
      swfobject.embedSWF(
        'http://www.youtube.com/v/<?php echo $YT_VideoID; ?>&enablejsapi=1&rel=0&fs=1',
        'ytplayer_div',
        '300',
        '250',
        '8',
        null,
        null,
        {
          allowScriptAccess: 'always',
          allowFullScreen: 'true'
        },
        {
          id: 'ytplayer_object'
        }
      );
    </script>
    <p><?php echo  substr(strip_tags($item->get_description()), 0 , 300) . "  \r\n"; //this line is the description if you like ?></p>
    <?php } // end if there is an item
    endforeach;
    endif; //success
    $feed->__destruct(); unset($feed);
     } // end Simplepie ?>
    <!-- end youtube video -->

    -
    Is that all? No, you have to replace the user_name with your User Name!!

    One line in this code renders the description of your video, if you do not like this, you are free to delete this line:

    <p><?php echo substr(strip_tags($item->get_description()), 0 , 300) . "  \r\n"; //this line is the description if you like ?></p>

    This code will create a valid XHTML embed code for the YouTube video using the free available swfobject library swfobject.js.

    Problems? Just Join and ask...

    Posted 1 year ago #
  2. greenbandit
    Member

    great post, any idea to use curl instead of simplepie?

    Posted 12 months ago #
  3. Sorry, I never used cURL yet.
    As simplepie is in WordPress already and is useful for many other functions to grab feeds, I use it as is.

    What I do is actually not disable the caching function, eg do not use:

    $feed->enable_cache(false); // disable caching

    but use instead:

    $feed->set_cache_location('cache'); // change to your cache location
    $feed->set_cache_duration(259200); // 3 x 24h x 3600sec (3 days)

    you just need a writable /cache folder in the root of WordPress and set the cache duration to what ever you like. If it's working you see as soon simplepie stores files in that folder.

    Posted 12 months ago #
  4. greenbandit
    Member

    hi, and thanks for a fast answer!

    I've reading this: http://simplepie.org/wiki/reference/simplepie/force_fsockopen

    Thanks.

    Posted 12 months ago #
  5. raf_79
    Member

    This is great! I got it to work without a problem following your instructions. What changes do I need to make to the code to make this work in a sidebar widget so I can control the pages it shows on? Thanks...

    Posted 11 months ago #
  6. I use it here Barthami Corporate Wordpress Theme

    This is not in a widget though, it just shows the embeded video of the latest posts of a category. It's totally different from the code above, does not need simplepie as you post it just like a normal post.

    I use an extra category 'videos' for that.

    Needs some functions in functions.php and some lines in sidebar, never made a widget from that.

    Should I 'walk you through'? I need some time to collect the code snippets out of the theme. - not today any more.

    Posted 11 months ago #
  7. raf_79
    Member

    Yes, please. I await your response - but not today. :)

    Posted 11 months ago #
  8. I opened a new topic thread for that:

    add latest video from a post in WordPress sidebar

    other ways to get video code from WordPress post or resize a video here

    get the oembed code of a video in a WP 2.9 post from postmeta

    or here

    Change width and height in oembed code on the fly in WordPress

    Posted 11 months ago #
  9. raf_79
    Member

    Thank you for the coding to add the latest video from a WP post on your site into a sidebar widget. This information will be very useful for me. :)

    I'd like to ask, and pray I don't offend you, does this mean that you can only pull those 4 stills (as the coding in an earlier post in this thread shows how to do) in from offsite using SimplePie outside a widget? What I was hoping to accomplish was pulling the stills from my YouTube channel and have them update automatically with the latest video stills from there as a teaser on my main site, but control the display pages. Is what I'm asking possible, and if it is, how would I accomplish this?

    Again, I thank you for your patience and I'm truly grateful for your continued help.

    Posted 11 months ago #
  10. Above in this thread you get your user-channel feed from YouTube as feed. Either you display 4 stills linking to the video in youtube (this changes if you add something to your channel)
    or
    you load the latest clip in a player as alternative. In this second choice with the player you could load more clips than one, to be set in this line:
    php if ($success): foreach ($feed->get_items(0, 1) as $item)
    get_items(0, 1) reads get from the first item (0) just one clip (1)

    you might try get_items(0, 4) to get 4 latest clips and or change the player size from '300','250' to any other size you like

    That's about your user channel feed.

    In the new post I use a very different way, taking any video of anyone from your latest post in the category you choose.
    That is explained here:
    http://forum.go41.de/topic/add-latest-video-from-a-post-in-wordpress-sidebar

    Posted 11 months ago #

RSS feed for this topic

Reply

You must log in to post.

Join us! or log in (lost password?):