Lee Willis

How to write an application for WordPress

| 3 Comments

I’ve recently been doing some work for a family member on a WordPress blog they run, and the need came up to build a mini-app. Now historically, I’m a Joomla kind of guy, and it’s fairly clear that for a Joomla site you would build a component to implement your app.

However, in WordPress it’s a little different. I’ve found loads of articles about available API calls, manipulating “the loop” and all sorts of useful information. What I haven’t found though is something that explains at a fundamental level how I should approach the problem.

There isn’t a concept of an “application” under WordPress, the only items that are accessible are “posts” and “pages”. As far as I can tell, I have two options:

  1. Publish a post/page with no content other than a special string (E.g. [my-app]) and then write a plugin that filters post content and replaces the magic string with the output from the application
  2. Create a page, and assign it a custom template. Implement your application in that custom template

Option 1

It works, I know because this is what I did 🙂   However there is a performance penalty, your application code is called on every post, and so you have to take steps to make sure it doesn’t run unnecessarily. The other disadvantage is that well, it just doesn’t seem clean.

Option 2

This seems a little nicer, but doesn’t really fit with WordPress’ nice plugin installation architecture – as far as I can tell plugins can’t auto-install into the current theme directory, and custom templates can’t live anywhere else.

So, this leaves me a little confused – can anyone point me as to how I should have solved this problem – or does WordPress need to grow up a little and add “application” alongside posts and pages?

3 Comments

  1. hiya, Lee, wordpress’s version of an ‘application’ is a ‘plugin’. You write your plugin, install & activate it, and then call it from wherever you want (eg. a sidebar, a particular post, your site-wide header, etc).

    Have a look around http://wordpress.org/extend/plugins/

    Hope that helps!

    dan

  2. ridiculous – just re-read your post & see that’s what you went for!

    hope life’s good,

    dan

  3. For Option 2, I guess you can use a combination of $wp_query, Rewrite API and template_redirect hook. That way the user won’t be required to create pages, and you can even allow customization of your plugin output (same way as customizing WordPress page, single or search templates).

Leave a Reply

Required fields are marked *.