I recently started attending meetings of the East Bay WordPress Meetup Group, including today’s fun WordPress Plugin Hackathon, hosted and presented by Website in a Weekend‘s Dave Doolin and TechLiminal‘s Anca Mosoiu. (That’s pronounced “muh-show-you”, as in, “WordPress is easy and fun. Here, let Mosoiu!” Sorry, Anca, I just couldn’t resist .) I learned some cool stuff about hacking WordPress (in a good way), and also got some advice for beginning bloggers: “you should blog every day!” And despite my newbie-ness, I was able to contribute a bit by helping others debug their PHP a little.
Well, I don’t know if I’ll achieve every day – nor that I really ought to! But let’s see if I can get myself in some kind of a habit, here. If you look at the dates on my posts, it’s obvious I’ve been a little sluggish getting into the swing of things. I will try to post more about what I’ve been up to lately. Let’s start with what I learned today!
Plugins, shortcodes, and custom fields. I much enjoyed Dave’s walk-through of a very simple plug-in. We first set up a development environment on our local machines. Easy as: 1. An apache/mysql/php stack (I already had WAMPServer running on my PC); 2: An editor (they recommended the Aptana Studio IDE which I installed for this occasion yesterday since I don’t do much development on this machine, but to be perfectly honest I missed the intro to using Aptana if there was one, and didn’t really feel like getting up to speed, so I just did this simple task in WordPad, which was perfectly adequate); 3: locally installed a test WordPress blog, rather than unleashing development code on this blog! Creating a simple plugin was pretty straightforward. You just create a PHP script in your wp-content/plugins directory, and it magically appears in the plugins page of your WP dashboard to be activated like any other plugin. Of course, it doesn’t do anything until you add some code.
Dave walked us through a simple plugin to add some special navigation to individual posts, for example if you have a series of posts on a single topic and you want to have links to the other posts in the series. It consists of three simple elements. First, you use two custom fields (a standard WP feature found on your Edit Post page) to indicate which other posts you want to link to as “previous” and “next”; each custom field has a name that the plugin will recognize, and a url for the post to link to. Then you write a PHP function in the plugin script that will recognize these custom fields and build some HTML containing links to those URLs. It shouldn’t produce any output to the page; instead, the function just returns the HTML as a string. Finally you just need a shortcode so that you can invoke this function from your blog posts. I’d heard of shortcodes before but this took the mystery out. They’re just a simple name that invokes the function you added to the plugin script. I named my shortcode “post-series”, so I just type that right into my post in square brackets (“[post-series]“), and it magically invokes the PHP function and is replaced by the HTML the function returns. And voilà!
Fun stuff! Thanks, Dave!