Created
January 30, 2012 19:03
-
-
Save sguzik/1706004 to your computer and use it in GitHub Desktop.
A simple widget for displaying posts from a tumblr widget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Brooklyn Ink Tumblr Widget | |
Plugin URI: http://thebrooklynink.com | |
Description: A simple widget for displaying posts from a tumblr widget. | |
Author: Sam Guzik | |
Version: 1.0 | |
Author URI: http://samguzik.com | |
*/ | |
/** | |
* tumblrFeed_Widget Class | |
*/ | |
class tumblrFeed_Widget extends WP_Widget { | |
/** constructor */ | |
function __construct() { | |
parent::WP_Widget( /* Base ID */'tumblrFeed_widget', /* Name */'Tumblr Feed Widget', array( 'description' => 'A widget for displaying Tumblr feeds.' ) ); | |
} | |
/** @see WP_Widget::widget */ | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
$feed = apply_filters( 'widget_feed', $instance['feed'] ); | |
$link = apply_filters( 'widget_link', $instance['link'] ); | |
$count = apply_filters( 'widget_count', $instance['count'] ); | |
echo $before_widget; | |
if ( !empty( $title ) ) { echo $before_title .'<a href="'.$link.'" target="_blank">' . $title .'</a>' . $after_title; } | |
?> | |
<div class="blogboxhib"> | |
<div class="blogtext"> | |
<?php | |
/** widget content begins here */ | |
$url = $feed; //where you can find the posts | |
$xml = simplexml_load_file($url); //parse the tumblr API to get an object containing all of your posts | |
$i = 0; //create a counter | |
foreach( $xml->posts->post as $post ){ //create loop to process individual posts | |
if ( $i < $count ) { //check that the loop hasn't run before | |
if ( $post['type'] == 'regular' ) { //check what kind of post we're dealing with | |
$post_title = $post->{'regular-title'}; //create a variable to store the title | |
$post_body = $post->{'regular-body'}; //create a variable to store the body | |
$post_excerpt = substr( strip_tags($post_body),0,320 ); //substring the body text to get your excerpt (while simultaneously removing all html) | |
echo '<h6 class="hib-hed">' . $post_title . '</h6>'; //output your formatted title | |
echo '<p class="hib-text">' . $post_excerpt . '...</p>'; //output your formatted excerpt | |
} elseif ( $post['type'] == 'photo' ) { | |
$post_photo_src = $post->{'photo-url'}[3]; //create a variable to store the photo source (specifically, the small square one) | |
$post_photo_caption = $post->{'photo-caption'}; //create a variable to store the photo caption | |
$post_photo_caption_small = substr( strip_tags($post_photo_caption),0,410 ); //create a variable to store the photo caption | |
echo ' <img style="width:220px" src="'.$post_photo_src.'"> ' ; // output your photo | |
echo ' <p class="hib-caption">' . $post_photo_caption_small . ' <a href="http://www.thebrooklynink.tumblr.com">...Read More</a></p>'; // output your photo caption (small version) | |
} elseif ( $post['type'] == 'quote' ) { | |
$post_quote = $post->{'quote-text'}; //create a variable to store the quote | |
$post_source = $post->{'quote-source'}; //create a variable to store the source | |
echo ' <h5 class="hib-quote">' . $post_quote . '</h5>'; // output your quote | |
echo ' <p class="hib-source">' . $post_source . '</p>'; // output your source | |
} elseif ( $post['type'] == 'link' ) { | |
$post_link_url = $post->{'link-url'}; //create a variable to store the link url | |
$post_link_text = $post->{'link-text'}; //create a variable to store the link text | |
$post_link_description = $post->{'link-description'}; //create a variable to store the link description | |
echo ' <a href=" ' . $post_link_url . ' "><h6 class="hib-link">' . $post_link_text . '</h6></a>'; // output the link | |
echo ' <p class="hib-link-description">' . $post_link_description . '</p>'; // output your link description | |
} elseif ( $post['type'] == 'video' ) { | |
$post_video = $post->{'video-player'}[2]; //create a variable to store the video | |
$post_video_small = preg_replace('/(width)=("[^"]*")/i', 'width="220"', $post_video); | |
$post_video_caption = $post->{'video-caption'}; //create a variable to store the video caption | |
echo ' <p class="hib-video">' . $post_video_small . '</p>'; // output your video | |
echo ' <p class="hib-video-caption">' . $post_video_caption . '</p>'; // output your video blurb | |
} elseif ( $post['type'] == 'audio' ) { | |
$post_audio_player = $post->{'audio-player'}; //create a variable to store the audio | |
$post_audio_caption = $post->{'audio-caption'}; //create a variable to store the audio caption | |
echo ' <p class="hib-audio">' . $post_audio_player . '</p>'; // output your video | |
echo ' <p class="hib-audio-caption">' . $post_audio_caption . '</p>'; // output your audio blurb | |
} elseif ( $post['type'] == 'conversation' ) { | |
$post_conversation_title = $post->{'conversation-title'}[2]; //create a variable to store the conversation title | |
$post_conversation = $post->{'conversation'}; //create a variable to store the conversation itself | |
echo ' <h6 class="hib-conversation_title">' . $post_conversation_title . '</h6>'; // output your conversation title | |
echo ' <p class="hib-conversation">' . $post_conversation . '</p>'; // output your conversation | |
} else { | |
echo 'YOU MESSED UP!'; | |
} | |
$i++; //shorthand for $i = $i + 1 | |
} | |
} | |
/** widget content ends here */ | |
?> | |
<a href="<?php echo $link; ?>"><p align="right"><strong>Read more of <?php echo $title; ?> »</strong></p></a> | |
</div> | |
</div> | |
<?php | |
echo $after_widget; | |
} | |
/** @see WP_Widget::update */ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['feed'] = strip_tags($new_instance['feed']); | |
$instance['link'] = strip_tags($new_instance['link']); | |
$instance['count'] = strip_tags($new_instance['count']); | |
return $instance; | |
} | |
/** @see WP_Widget::form */ | |
function form( $instance ) { | |
if ( $instance ) { | |
$title = esc_attr( $instance[ 'title' ] ); | |
$feed = esc_attr( $instance[ 'feed' ] ); | |
$link = esc_attr( $instance[ 'link' ] ); | |
$count = esc_attr( $instance[ 'count'] ); | |
} | |
else { | |
$title = __( 'New title', 'text_domain' ); | |
$feed = __( 'http://', 'text_domain' ); | |
$link = __( 'http://', 'text_domain' ); | |
$count = __( '1', 'text_domain'); | |
} | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> | |
<label for="<?php echo $this->get_field_id('feed'); ?>"><?php _e('Feed:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('feed'); ?>" name="<?php echo $this->get_field_name('feed'); ?>" type="text" value="<?php echo $feed; ?>"/> | |
<label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Read More Link:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>"/> | |
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Items to Display:'); ?></label> | |
<input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>"/> | |
</p> | |
<?php | |
} | |
} // class Foo_Widget | |
// register Foo_Widget widget | |
add_action( 'widgets_init', create_function( '', 'register_widget("tumblrFeed_widget");' ) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment