How to: a recent posts plugin for e-Newsletter

A bunch of you have posted over time, asking for a way to include recent posts in your e-Newsletter mailings. In my recent aborted foray into using e-Newsletter for my mailings, I wound up writing a recent posts plugin (code below).

Here's how it works. First, you need the hacked version of e-Newsletter that supports shortcodes from here.

Next, you'll need to cut and paste the code below into a file named something like recent-posts.php in a folder named something like recent-posts, and drop it into your plugins. Then, use the shortcode in the form:

[e-newsletter-recent-posts limit="30"]

You can leave the limit parameter out if you want.

Now, here are the cautions. This code is unsupported and I'm not using it. In fact, as my long discussion about e-Newsletter shows, I've given up on using e-Newsletter and am going back to rolling my own solution (it's ugly, don't ask).

Anyway, the point is, using this plug-in is something you do at your own risk. Using it could cause you to have babies (if you don't want them), or not be able to have them (if you want them). It could knock the world off its axis. It could cause baldness. It could cause your pee to smell. Baaaaaad things could happen. It will not be supported, it will not be updated, it will not solve your problem, it will not get you a date, and it will not make you a happy person. It will not bring about world peace and it will not make the world want to eat its peas.

It is, merely, a slab of barely tested, horribly under-loved code. It is the Pauly Shore of plug-ins.

So, there you go. Good luck. May the farce be with you. Don't blame me if you your wife leaves you, you lose your home, or you become uncontrollably flatulent.

Oh, and I have no idea if it works in multisite or not. I tried it once, found the problem that made me dump e-Newsletter, and went on my merry way. My guess is it works in each site, for that site. But, you won't know until you try it, and don't tell me. My apathy knows no bounds when it comes to this project.

<?php
/*
Plugin Name: e-Newsletter Shortcodes
Description: This plug-in provides shortcodes for mailing newsletters for e-Newsletter. This is unsupported, probably won't work, and could make your pee smell.
Version: 0.0.1
Author: David Gewirtz
Plugin URI: http://zatz.com/info/zenpress/
Author URI: http://zatz.com/info/zenpress/
License: GPL2
*/

function e_newsletter_recent_posts_shortcode( $atts ) {
	// Thanks to http://rhanney.co.uk/2010/08/26/simple-recent-posts-shortcodes-for-wordpress/ for hints getting started
	extract( shortcode_atts( array( 'limit' => 5 ), $atts ) );

	$q = new WP_Query( 'posts_per_page=' . $limit );

	$list = '<div class="latestposts">';

	while ( $q->have_posts() ) {
		$q->the_post();
		$list .= '<H3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
		$list .= '<div class="latestspost_content"><p>' . get_the_excerpt() . '</p></div>';
	}

	wp_reset_query();

	return $list . '</div>';
}

add_shortcode( 'e-newsletter-recent-posts', 'e_newsletter_recent_posts_shortcode' );
?>