Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've hacked together a simple plugin to show the latest image from a Tumblr blog.

To avoid upsetting the Tumblr API (v1), I want to cache that image within Wordpress...

I think perhaps I can do this somehow using the Transients API, but have no idea where to start with this... or is there a simple way to do it with straight up PHP?

Thanks :)

<?php
/*
Plugin Name: Topshop Tumblr Widget
Plugin URI:  
Description: Displays images from the Topshop Tumblr using their v1 API
Author: James Greig
Version: 0.1
Author URI: http://greig.cc
*/

/*
Based on the Tumblr plugin by V.J.Catkick - http://www.vjcatkick.com/
Data source: http://topshop.tumblr.com/api/read?start=0&num=1&type=photo

*/


function widget_topshop_tumblr() {
    if ( !function_exists('register_sidebar_widget') )
        return;

    function widget_topshop_tumblr_photos( $args ) {
        extract($args);

        $options = get_option('widget_topshop_tumblr_photos');
        $tuid = $options['tumblr_recents_src_uid'];

        $output = '<div id="tumblr_recent_photos">';
        $output .= '<a href="http://topshop.tumblr.com/" class="logo-tumblr"></a>';
        // $output .= '<a href="#" class="arrow-left"></a>';
        // $output .= '<a href="#" class="arrow-right"></a>';


if ( function_exists('simplexml_load_file') ) {

    $tumblr_userid = $tuid;
    $tumblr_num = '1';
    $img_style = $tumblr_recents_img_style;
    $tumblr_size = '250';
    $pagecounter = 0;

    $_tumblrurl = 'http://' . $tumblr_userid . '.tumblr.com/api/read?start=' . $pagecounter . '&num=' . $tumblr_num . '&type=photo';
    $_tumblrurl  = urlencode( $_tumblrurl );
    $_tumblr_xml = @simplexml_load_file( $_tumblrurl );

if( $_tumblr_xml && $_tumblr_xml->posts[0] ) {

    // 0.1.2
    if( $tumblr_recents_additional_html ) {
        $output .= str_replace( "\\","", $tumblr_recents_additional_html );
    }

    foreach( $_tumblr_xml->posts[0]->post as $p ) {
        $photourl = $p->{"photo-url"}[3];
        $linkurl = $p[url];
        $output .= "<div class='image-tumblr'>";
        // $output .= '<a href="' . $linkurl . '" target="_blank" >';
        $output .= '<a href="http://topshop.tumblr.com/" target="_blank" >';
        $output .= '<img src="' . $photourl . '" border="0" alt="' . $linkurl . '" />';
        $output .= '</a>';
        $output .= '</div>';
    }
}

} else {
    $output .= 'Requires PHP 5.1';
}

        $output .= '</div>';

        // These lines generate the output
        echo $before_widget . $before_title . $title . $after_title;
        echo $output;
        echo $after_widget;
    }

    function widget_topshop_tumblr_photos_control() {
        $options = $newoptions = get_option('widget_topshop_tumblr_photos');
        if ( $_POST["tumblr_recents_src_submit"] ) {
            $newoptions['tumblr_recents_src_uid'] = strip_tags(stripslashes($_POST["tumblr_recents_src_uid"]));
        } /* if */
        if ( $options != $newoptions ) {
            $options = $newoptions;
            update_option('widget_topshop_tumblr_photos', $options);
        } /* if */

        // those are default value
        if ( !$options['tumblr_recents_src_uid'] ) $options['tumblr_recents_src_uid'] = 'topshop';
        $tuid = htmlspecialchars($options['tumblr_recents_src_uid'], ENT_QUOTES);
?>

        <?php _e('Tumblr ID:'); ?> <input style="width: 100px;" id="tumblr_recents_src_uid" name="tumblr_recents_src_uid" type="text" value="<?php echo $tuid; ?>" />.tumblr.com<br />

        <input type="hidden" id="tumblr_recents_src_submit" name="tumblr_recents_src_submit" value="1" />

<?php
    } /* widget_topshop_tumblr_photos_control() */

    register_sidebar_widget('Topshop Tumblr Widget', 'widget_topshop_tumblr_photos');
    register_widget_control('Topshop Tumblr Widget', 'widget_topshop_tumblr_photos_control' );
} /* widget_topshop_tumblr() */

add_action('plugins_loaded', 'widget_topshop_tumblr');

?>
share|improve this question
    
I would guess you could store the latest image in a special folder in wp-content(to avoid having another image with the same name) and then use the tumbler API to compare that image with the latest image on the tumblr blog at a specific interval. –  fdsa Jul 2 '12 at 2:57
    
Take a look how this plugin implements its image caching feature: plugins.svn.wordpress.org/get-the-image/trunk/get-the-image.php :::: Are you James Greig?, user7528 is so robotic... ;) –  brasofilo Jul 2 '12 at 7:17
    
Fair point, I am no longer robotic :) –  3stripe Jul 2 '12 at 12:52
    
I use this plugin: vjcatkick.com/?page_id=3008 –  Barry Carlyon Jul 15 '12 at 0:16
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.