Wishful Coding

Didn't you ever wish your computer understood you?

Wordpress RSS widget in a post/page

I'm currently writing a theme for a friend who is going to coach photographers. He wants to have a page that lists RSS feeds from all those photographers. One part of the puzzle is easily solved by using Yahoo! Pipes to combine those feeds, the other part isn't that easy to solve. WordPress comes with a standard RSS widget, but it's only a widget, not a shortcode. There are a bunch of plugins that either offer a RSS widget or turn feeds items into posts. What I did is write my own plugin in only 10 lines of actual code, utilizing the shortcode API and the built-in RSS widget.
  1. Save and upload the snippet below to your plugins folder
  2. Activate it in the WP admin
  3. Add the [rsstag] shortcode to your post or page
  4. Add the feed url like this: [rsstag url="http://pepijn.cqhosting.nl/feed/"]
  5. Save!
You can optionally add one or more of show_summary=1, show_date=1 or show_author=1 to add the respective meta-data to every entry.
php
/*
Plugin Name: Wordpress RSS shortcode
Plugin URI: http://pepijn.cqhosting.nl/2010/06/wordpress-rss-widget-in-a-post-page
Description: This plugin fetches an RSS feed as a shortcode
Author: Pepijn de Vos
Version: 1.0
Author URI: http://pepijn.cqhosting.nl
*/

function rsstag($atts) {
        $atts = shortcode_atts(array(
                'url' => get_bloginfo('rss2_url'),
                'show_author' => 0,
                'show_date' => 0,
                'show_summary' => 0
        ), $atts);
        //var_dump($atts);
        wp_widget_rss_output($atts['url'], $atts);
}
add_shortcode('rsstag', 'rsstag');
?>
I will upload this to WordPress.org soon.
Posted

Wordpress.com Stats: Top Posts Widget

This hack is so cool and so simple, I expect anyone to run up to me and yell that this has been done before. I created a 'Top Posts' sidebar widget based on the widget that comes with any Worpdpress.com blog. I know there are several widgets like this, but they all include their own statistics tracking. Mine is nothing more than an excerpt of the Dashboard widget that comes with the Wordpress.com Stats plugin. For people not interested in technical details, here is a plugin file, that works as usual; Make sure you install the Wordpress.com Stats plugin first! I said this hack was simple, here is way: The code is already there! I'm surprised Wordpress did not include the widget themselves. Lets have a look at stats.php: Line 925-926:
foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $post )
        $post_ids[] = $post['post_id'];
Line 942-949:
<?php foreach ( $top_posts as $post ) : if ( !get_post( $post['post_id'] ) ) continue; ?>
<p><?php printf(
        $printf,
        '' . get_the_title( $post['post_id'] ) . '',
//                        '' . $post['post_title'] . '',
        number_format_i18n( $post['views'] )
); ?></p>
<?php endforeach; ?>
My resulting guesswork looks like this:
<h4>Top posts</h4>
<ul>
<?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
        <li></li>
<?php endforeach; ?>
If anyone is interested, I can upload this to the Wordpress plugin repository. Done: http://wordpress.org/extend/plugins/wordpresscom-stats-top-posts-sidebar-widget/
Posted