On $wpdb->prepare

If you do the following, you’re a terrible person:

$query = "SELECT ID from {$wpdb->posts} WHERE ID = %d";
$query = $wpdb->prepare( $query, $value );

There is absolutely no reason for prepare to be on a separate line.

Here’s how you do it the right way:

$query = $wpdb->prepare( "SELECT ID from {$wpdb->posts} WHERE ID = %d", $value );

One line. Simple, easy to read, and not confusing.

#hackTO v2.0: WP InstaPaywall

#hackTO the second happened this past Saturday (Sept 26, 2010), and like the first one, it was awesome. We got a fresh batch of APIs (though, some returning) to play with, way too much sugar, and a lot of new faces, but the formula was the same: 6-7 hours to hack together an app using one (or more) of the given APIs.

The pressure was on: as former champion of #hackTO, I had to defend my title. I decided to go big with an ambitious idea, not totally sure if I’d be able to finish in the given time. I had originally come up with the idea at the first #hackTO, but didn’t really have time to implement so I put it on my projects-for-the-future list. But this time around figured I’d give it a shot and so the WordPress InstaPaywall plugin was born.

Sadly, I didn’t reclaim my title but managed to score a third place entry. The judges apparently had a tough time deciding though, because there were a lot of really great apps developed.

Now, onto the plugin!

WP InstaPaywall


In its simplest form, the plugin lets you paywall your blog posts and make money by charging readers for full access to them. Paywalls are the new hotness these days and Freshbooks’ billing API seemed like a perfect fit for accomplishing this.


I think it’s important to note that I hate paywalls. I hate freewalls even more (yes, NYTimes.com, I’m talking about you!). But, it’s a model that some people think will work (and are betting their futures on), and it clearly is working for some. So, while it pains me to build something that contributes to the paywall madness — I would much rather see everyone take a Guardian-like approach; the world would be a better place if we did — it doesn’t hurt to sip the kool-aid every once in a while.

Plugin Walk-through

The plugin basically takes a modified iTunes-like approach: 1-click buying that you’re billed for at the start of the month.

It’s a bit difficult to run a demo, so I’ll just use screenshots instead.

Note: The plugin is a bit rough around the edges and not quite as good-looking or elegant as I would like, but what do you expect in 6.5 hours?

Enable Paywall

WP InstaPaywall — Paywall Metabox

WP InstaPaywall — Paywall Metabox

The plugin adds a new metabox to the Edit Post screen, which let’s you enable the paywall for any given post. Once enabled, you can specify the amount that you would like to charge for the post.

Users => Freshbooks

WP InstaPaywall - Clients in Freshbooks

WP InstaPaywall - Clients in Freshbooks

All users in WordPress get an associated Client entry created in Freshbooks.

Recurring Invoice

All users in WordPress also get a Recurring Invoice entry created in Freshbooks. This is central to the application billing service in Freshbooks. This invoice is set to go out at the first of every month.

Rack up charges

WP InstaPaywall - A Paywalled Post

WP InstaPaywall - A Paywalled Post

Whenever a user encounters a paywalled post, they’re shown a message indicating they can view the full content by paying a given amount. If the user agrees to pay, the amount indicated is added as an entry to their Recurring Invoice.

WP InstaPaywall - An Unlocked Post

WP InstaPaywall - An Unlocked Post

WP InstaPaywall - Upcoming Payments

WP InstaPaywall - Upcoming Payments

Charges for any given month add up. Users can view upcoming charges as well.

WP InstaPaywall - Monthly Recurring invoice

WP InstaPaywall - Monthly Recurring invoice

On the first day of the following month, Freshbooks automatically emails the invoice to the user (Freshbooks has snail mail support if you want to go old school — though, this wasn’t integrated). Then, thanks to the magic of webhooks (and a just-in-case cron job), the user’s invoice entries are cleared so they have a clean slate for the next month.

And then the cycle repeats.

The Pipe Dream (aka Potential Improvements)

My origin scope for the plugin was way bigger, but I just didn’t have enough time to implement everything. My main goal was to provide a three-pronged approach to paywalling to allow content creators the maximum flexibility. Some other ideas I had for payment models:

  • The Subscription Model: Pay a single fee on an ongoing basis (weekly? monthly?) and get access to all content on the site.
  • The Debit Model: Load up your account with money up-front and charge on a per-use basis
  • The (FB) Credit Model: Similar to The Debit Model, but using Facebook Credits instead of money.
  • The (True) Micropayments Model: Let users decide how much they want to pay, and allow content creators to set “suggested amounts”.

Other improvements and crazy ideas:

  • Various Payment Periods: Options to select how often the user should be billed: Weekly, Monthly, Instantly?
  • Auto-billing: Rather than waiting till the end of the month for people to pay, integrate with Freshbooks’ autobilling support to charge their credit cards instantly.
  • Track and Suspend Deadbeats: If working with the “Pay me at the end of the month” model, add safeguards to monitor, suspend and harass people who fall behind on payments.
  • Authenticated RSS Feeds: This would only work with the subscription model.
  • Analytics: Probably the most important thing next the actual payment piece. Graphs and other metrics that tell you what content is being paid for, when, where, why, by whom, and so on.
  • Ditch Freshbooks? I love Freshbooks, but it’s way too expensive for Joe Blogger. For this plugin to work effectively, you’d need to get one of the higher-priced accounts which start at $30/month, which means you’d have to “sell” at least $30 worth of blog posts per month just to break even. It might be easier just to integrate directly with something like PayPal. But then again, Joe Blogger probably shouldn’t be paywalling his posts anyway…


Like most of my work (e.g. WordPress projects), I’m always happy to openly release the course code. However, the code for InstaPaywall in its current state is way too “unclean” to be release-worthy. When I get a chance, I’ll clean it up and open it up to the world.

Other HackTO News

Update (2010-11-01): I recently found out about Flattr which is an existing micro-payment platform for content creators. Very cool!

WordPress Plugin: HTML Emails

Tired of all those boring emails that you get from WordPress? Do you dread opening emails from WordPress because plain text and/or over-querystringified links terrify you? Rest easy friend, HTML emails for WordPress are here.

HTML Emails replaces the standard WordPress emails with spruced up versions that simply look good. Sample included below:

New Comment Notification as seen in Gmail

Currently, only comment notifications are HTML-ized (new comment and comment moderation emails), but I’m hoping to add all other email notifications soon (and yes, that includes Multi-Site emails coming with WordPress 3.0).

While I have only tested the emails on Gmail, Gmail on Android, and Outlook, they should work on most email clients (including clients without HTML support). If you’re using a client other than the 3 I’ve listed, I would appreciate an email with info on whether the email looks like it should and works correctly.

Don’t like my design chops? (Go ahead, admit it!) I’ve got you covered! HTML emails makes it easy to fully customize the look and feel of emails sent by WordPress. See the Other Notes section on the Plugin page for details on how to customize emails. More detailed walkthroughs to come.

Grab it from the Plugin Directory or install directly from WordPress (Plugins > Add New, search for HTML Emails).

Edit Flow v0.3: Usergroups and improved notifications

Edit Flow was bumped up to v0.3 last week and saw a flurry of other updates as bugs cropped up that I had managed to miss during the testing phase before release. The main focus of this release was to introduce usergroups, which will form the basis of future features and to enhance the notification functionality that was introduced in the previous version.

If you haven’t upgraded yet, download it from the Plugin Directory or directly from within WordPress.

Here’s a quick breakdown of the new features introduced in this release:


Version 0.3+ adds in what are called usergroups. On the outset, they’re similar to “Roles” built into WordPress, except that (at this stage) usergroups are simply ways to associate groups of users together. Edit Flow adds a number of sample usergroups for you to get started (as shown above) and get a sense of what sort of groupings you can create. However, the main power of usergroups comes with…

Notification Controls

Much of the feedback Edit Flow received since the email notification were introduced centred around having greater control over who receives notifications. Previously, post updates were emailed to authors, editorial commenters, and any roles that had been selected to receive notifications. Many people were drawn to the notification feature but were forced to keep it disabled since they didn’t want all their editors or administrators notified on every single post update.

With the new release, you can specify on a post level, what users and usergroups should receive notifications, so that only relevant people and groups of people receive updates.

Note: with the introduction of this feature the “Notify by Role” option was removed. In its place, a new feature was added “Always notify admin option” which includes the blog administrator in all notifications. To all overly protective, nosy admins that want to know everything: you’re welcome :)

This is just the beginning of notifications. Some interesting ideas that I’d like to integrate in future versions of Edit Flow include:

  • Giving users the ability  to subscribe to posts themselves
  • Have specific users or usergroups automatically subscribed to posts based on categories or tags assigned to posts.
  • Make the UI a bit more efficient. The UI for this new feature is something that was unfortunately rushed. My original vision didn’t make it in (due to various impracticalities, changes, and lack of time), but it’s very much a high priority on my list to make it easy to select users/usergroups (especially for installs with hundreds and thousands of users).

More Useful Notifications

On the topic of notifications, the new release introduces emails that are slightly more descriptive in terms of the action taken on the post. The subject line of the email will specify whether the post was created, published, unpublished, etc. Although a small change, it should hopefully help users manage incoming emails more effectively and not get inundated with a barrage of “Post Status was changed” emails. (Interestingly, I’ve found that this new change comes in handy even on my personal blog which is a simple on-user blog. I find these notifications fairly useful especially since I make aggressive use of WordPress’ future scheduling functionality.)

Additionally, the action links in comment notifications now take the user directly to the editorial comment form (e.g. clicking on “Add editorial comment” will open the post and take to directly to the Editorial Comment form). Again, not a major feature but something that should hopefully save you some time, scrolling and future dealings with Carpal Tunnel.

I’d like to extend this feature even further and allow users to reply to comments via email and not have to go into WordPress to do so. (As you can see, there’s a bit a time-saving trend going on here.)

New widget: Posts I’m Following

Still a little crude at this stage, this new widget gives you a list of the most recently updated posts that you’re following. However, this widget will likely form the basis of the activity stream, which will offer an audit trail of activity happening within the WordPress admin.

Knight News Challenge Round II

While not really a feature introduced in 0.3+, here’s a bit of news that may be interest: we’ve submitted our 2nd round application for the Knight News Challenge. Check out it, vote, and leave us some feedback.

What’s Next?

Apart from some of the ideas already mentioned, with the next couple of Edit Flow releases, you can expect to see some great features such as:

  • Post task lists (à la Basecamp, namely a list of tasks that must be completed in order for a post to be published)
  • Better Post Management (to help you track and manage your content better, such as snapshots of how far along existing content is)
  • HTML emails (because emails should always be pretty – but always fallback to plain text for people still living in the ’90s)

Your Homework

As always, your feedback is much appreciated and vital to our development. Let us know what about Edit Flow works for you and what doesn’t and what else Edit Flow can do to improve your organization’s WordPress experience.

We’ve already had discussions with several online and print publishers and newsrooms interested in adopting Edit Flow and would love to include you in that conversation. Why not get in touch?

WordPress plugin: Feedkillah

Update (2010-01-09): So turns out wpengineer.com had this figured out a while back (i.e. October 2008). Regardless, it was a good opportunity for me to dig into the WordPress core and figure out what’s going on.

…or in “proper” English: Feed Killer.

Basically, a small plugin to disable all your feeds on your WordPress install, for whatever crazy reason; we don’t judge (much). This is a result of a question by @wesbos.

There’s also a non-plugin version if you don’t want to go the plugin route (for whatever crazy reason; again, we don’t judge (extensively)). Just add the code to your theme’s functions.php file.

See below or download from gisthub.

Continue reading

Edit Flow/WordPress Idea: User Directory

Update: sorry, should have pointed out that this idea was inspired by wireframes posted by Anthony Pesce (of the Populous project).

Here’s an idea that I thought might be a useful addition to Edit Flow especially once we add in User Group functionality. The main premise is to have a full-out User Directory accessible by logged in users that provides easy access to contact information for other users on the site. Would probably only be useful for larger groups using WordPress, but I’d imagine would still come in handy for newsrooms and such.

Continue reading

WordPress Plugin: Plugin Notes

This is the outcome of a couple of hours of Friday night coding after an open call by Chris Coyier over at Digging into WordPress. Here’s what Chris asked for:

Ever look through your list of plugins and forget just exactly what one of them does? I know they have descriptions next to them, but that doesn’t always speak to exactly what you are using it for and why. This plugin would just put a text field in each plugin field you could type some notes in there, theoretically to keep information about why and how you are using this plugin.

And since I was bored (and thought this was a pretty useful idea), I delivered. Plugin Notes is exactly what it sounds like.

Once you install and activate, the plugin adds a link “Add plugin note” that lets you add in a little note next to each plugin. It’s totally ajaxified and full of cool goodness. (Unfortunately, I was a bad programmer and didn’t make plugin gracefully degrade when javascript is turned off. Sorry, folks. Maybe next time.)

Plugin Notes: adding a note is easier than milking a cow.

Plugin Notes: adding a note is easier than milking a cow.

When a note is added, it shows up inside a little blue box and includes the name of the user that added the note as well as the date and time when the note was added. You also get handy dandy options to “Edit” or “Delete” notes.

WordPress plugin: Plugin Notes - blue boxes make things look pretty.

WordPress plugin: Plugin Notes - blue boxes make things look pretty.

Each plugin can only have one note. I can imagine there would be cases where multiple notes may come in handy, but those would be rare so I’m passing on that functionality.

The plugin is pretty simplistic, and unlikely to see any future feature additions (unless someone really, really wants one). I’ll keep a watch for compatibility with future versions of WordPress though, so rest easy.

Excited, enough? Grab Plugin Notes (from the WordPress Plugin Directory) or download it from within WordPress.

Note: you’ll need PHP5 and a javascript-enabled browser for the plugin to work.

WordPress Tip: Using the same template across multiple category, tag, and author pages

Sharing Category Templates

Sharing Category Templates

Update (2009-12-21): So, the original technique that I had posted is a bit hacky and kinda gets annoying when you have lots of categories and category templates. There’s a better approach that can add to your functions.php file to make everything work magically, which was inspired by Justin Tadlock‘s comment on a post by Elliot Jay Stocks. Code below:

Add to your theme’s functions.php file and edit as necessary. For example, to redirect to categories with IDs 1 & 5 to a template called gallery.php, change the if statement to:

if( is_category( array( 1, 5 ) ) )
    template = locate_template( array( 'gallery.php', 'category.php' ) );

Easy, no?

Note: the stuff below is old, crappy, “legacy” code. Still here for posterity…

WordPress has an amazing theming system. It’s not perfect, but if you know the right things you can bend it to your will and do a lot of cool things. One great thing about it is the template hierarchy; WordPress basically allows you to create custom templates for categories, tags, authors, etc.

For example, if I add category.php to my theme, all category pages will now be styled according the structure within the category.php file. Now, let’s say I have a category called “Photos” that I wanted to look a little different from the rest. (Let’s assume the category ID for “Photos” is 6). All I have to do is add category-6.php to my theme, modify it to whatever, and the “Photos” category page will switch to this new template. Cool? Definitely.

With a system like this, you could technically customize your site to the point where the every category, author, tag, etc. page has a unique look and feel. But, that’s quite a lot of work and in most cases unnecessary. There may be cases though, where you want a small subset of categories to have a unique look, and you want these pages to share this look. Unfortunately WordPress doesn’t allow different pages to share templates. (Intuitively, I would think that something like creating a file called category-6,7,8.php [or similar] would apply this template to categories 6, 7, and 8). You could technically set up separate files (category-6.php / category-7.php / category8.php) and just copy and paste the code across all of them, but that’s a maintenance nightmare. A single change will have to be copied over multiple times, and that’s just annoying.

There are a few ways around this. One of easy ways is to use Idealion’s Category Enhancements plugin. Alternatively, if you want a more theme-level solution, follow the steps below. This is something I discovered while building out the image gallery pages for The Boar (links of what I came up with are at the bottom).

1. In your theme folder, create the category files for the categories you want to apply the custom template to.

  • Let’s assume we want to apply it to the categories “Portrait Photography” (ID: 5) and “Nature Photography” (ID: 12).
  • In this case we’d create category-5.php and category-12.php

2. In the category template files (category-5.php & category-12.php), add the following:

<?php require_once('common_template.php'); ?>

3. Create a generic category template: common_template.php

4. Then in the common_template.php, add the following:

<?php get_header(); ?>
<?php echo 'Hello World!'; ?>
<!-- The rest of your custom template goes here -->
<?php get_footer(); ?>

5. Now, if you navigate to http://your-site.com/?cat=5 and http://your-site.com/?cat=12, you’ll notice that their look will mirror what you included in common_template.php

I should note that this approach isn’t limited to category templates; you can use this with author and tags templates as well. Technically, with this approach you could combine multiple categories and tags, as well (though if you do, be wary of category- and tag-specific functions and make use of conditional tags).

You can see an example of this in action at the links below:

Sample files you work from. Download them, rename the extension to .php and throw them into your template folder.

Thoughts, questions, suggestions? Leave a comment below or send me an email.