Skip to content

Instantly share code, notes, and snippets.

@sivel
Created March 27, 2009 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivel/86762 to your computer and use it in GitHub Desktop.
Save sivel/86762 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: feed-pseudo-widget
Plugin URI: http://wordpress.org/#
Description: feed-pseudo-widget
Author: Matt Martz
Version: 1.0
Author URI: http://sivel.net/
*/
function feed_pseudo_widget($atts, $content) {
extract(shortcode_atts(array(
'url' => false,
'count' => 10,
'title' => true,
'title_before' => '<h3>',
'title_after' => '</h3>',
'before' => '<ol>',
'after' => '</ol>',
'link_before' => '<li>',
'link_after' => '</li>',
'cache' => false,
'timeout' => 3600,
'description' => false,
'content' => false,
'meta' => false,
'date_format' => 'd M Y H:i:s',
'width' => null,
'height' => null,
'float' => 'none'
), $atts));
$arrURL = parse_url($url);
if ( $url == false || $arrURL == false ) {
return "<p>Malformed or no feed URL provided.</p>";
}
$boolvals = array('title', 'cache', 'description', 'content', 'meta');
foreach ( $boolvals as $boolval ) {
if ( $$boolval == "true" )
$$boolval = true;
else
$$boolval = false;
}
$strintvals = array(
array('attr' => 'count', 'default' => 10, 'type' => 'int'),
array('attr' => 'title_before', 'default' => '<h3>', 'type' => 'string'),
array('attr' => 'title_after', 'default' => '</h3>', 'type' => 'string'),
array('attr' => 'before', 'default' => '<ol>', 'type' => 'string'),
array('attr' => 'after', 'default' => '</ol>', 'type' => 'string'),
array('attr' => 'link_before', 'default' => '<li>', 'type' => 'string'),
array('attr' => 'link_after', 'default' => '</li>', 'type' => 'string'),
array('attr' => 'timeout', 'default' => 3600, 'type' => 'int'),
array('attr' => 'date_format', 'default' => 'd M Y H:i:s', 'type' => 'string'),
array('attr' => 'height', 'default' => null, 'type' => 'string'),
array('attr' => 'width', 'default' => null, 'type' => 'string'),
array('attr' => 'float', 'default' => 'none', 'type' => 'string')
);
foreach ( $strintvals as $strintval ) {
if ( $strintval['type'] == 'int' )
$$strintval['attr'] = (int) $$strintval['attr'];
if ( $strintval['type'] == 'string' )
$$strintval['attr'] = (string) $$strintval['attr'];
if ( ( $strintval['type'] == 'int' && !is_int($$strintval['attr']) ) || ( $strintval['type'] == 'string' && !is_string($$strintval['attr']) ) || $$strintval['attr'] === 0 )
$$strintval['attr'] = $strintval['default'];
}
if ( !class_exists('SimplePie') )
include('simplepie.inc');
$feed = new SimplePie();
$feed->set_feed_url(array($url));
if ( $cache == true && ( !is_dir(WP_CONTENT_DIR . '/rss_cache') || !is_writable(WP_CONTENT_DIR . '/rss_cache') ) )
$cache = false;
$feed->set_cache_location(WP_CONTENT_DIR . '/rss_cache');
$feed->set_cache_duration($timeout);
$feed->set_timeout(5);
$feed->enable_cache($cache);
$feed->strip_htmltags(array('script'));
$feed->set_item_limit($count);
$feed->init();
$feed->handle_content_type();
$items = $feed->get_items();
if ( !isset($items[0]) )
return "<p>Feed could not be retrieved.</p>\n";
unset($feed);
$output = "<div class='feed-pseudo-widget'";
if ( !empty($height) || !empty($width) || !empty($float) ) {
$output .= " style='";
if ( !empty($height) )
$output .= "height:$height;";
if ( !empty($width) )
$output .= "width:$width;";
if ( !empty($float) )
$output .= "float:$float;";
$output .= "'";
}
$output .= ">\n";
if ( $title == true )
$output .= "$title_before<a href='{$items[0]->get_feed()->get_permalink()}' title='{$items[0]->get_feed()->get_title()}'>{$items[0]->get_feed()->get_title()}</a>$title_after\n";
$output .= $before;
foreach ( $items as $item ) {
$output .= $link_before . "\n";
$output .= "<a href='{$item->get_permalink()}' title='{$item->get_title()}'>{$item->get_title()}</a>\n";
if ( $meta == true )
$output .= "<span class='feed-pseudo-widget-meta'>-by {$item->get_author()->get_name()} on {$item->get_date($date_format)} GMT</span>\n";
if ( $content == true )
$output .= "<div class='feed-pseudo-widget-content'>{$item->get_content()}</div>\n";
if ( $description == true && $content != true )
$output .= "<div class='feed-pseudo-widget-description'>{$item->get_description()}</div>\n";
$output .= $link_after. "\n";
}
$output .= $after;
$output .= "</div>\n";
return $output;
}
add_shortcode('feed', 'feed_pseudo_widget');
add_action('wp_head', 'feed_pseudo_widget_css');
function feed_pseudo_widget_css() {
?>
<style type="text/css">
.feed-pseudo-widget {
border: 3px solid lightgrey;
-moz-border-radius: 10px;
-khtml-border-radius: 10px;
-webkit-border-radius: 10px;
padding: 0 10px 0 10px;
margin: 10px 0 10px 0;
}
.feed-pseudo-widget h3 {
border-bottom: 1px solid lightgrey;
}
.feed-pseudo-widget-meta {
font-size: 85%;
}
</style>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment