Wishful Coding

Didn't you ever wish your
computer understood you?

Wordpress.com Stats: Top Posts Widget

Wordpress.com Stats: Top Posts WidgetThis 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/

Food versus code

Food: 279kJ/66kcal; E263, E181, E459;
Code: [a-zA-Z0-9]*; hey
Now, which one is easier to decipher? Do you think we need simple food, simple code, or both? For the record, the first food part is from coffee, the ingredients are Calcium acetate, Tannin and Beta-cyclodextrine. The first code part is a regex matching any number of alpha-numeric characters, the second part is 'hey' in html entities.

Shorten Wordpress urls in Adium

I use Adium as my Twitter client, it includes a nice url shortener supporting multiple services, so what is my problem? Nothing, I just had some fun making use of the wp.me service. Every Wordpress blog contains a header containing the short url, so it was only 3 lines of Applescript and one line of Bash to get the short url of any wp blog and post it to Adium. If anyone with knowlegde of regex/grep comes around, he might get it to work with any shorturl/shortlink header.

Shorten Wordpress urls in Adium

The current line looks like this:
curl --head <url> -s | grep -o http://wp\.me/[a-zA-Z0-9\-]*
Update: sed seems to be able to do what I want.
curl --head http://pepijn.cqhosting.nl/ -s | \
sed -n 's/Link: <\(.*\)>; rel=shortlink/\1/p'
Use:
%_shorten{<url>}
Download the script: shorten.AdiumScripts