As a coder I love WordPress. The open-source community which has contributed to its development has succeeded in making it endlessly customizable, which is why it’s used nowadays for much more than blogging.

However, the process of learning how to write custom WordPress code — for plugins, themes, even simple adjustments — can be, well, rocky. Unlike software packages that have been developed and finessed by a single entity (how great was Actionscript?), WordPress is a constantly-changing broth with many cooks. Functions, hooks and variable names are idiomatic, methods and techniques are slippery, what works here doesn’t work there … it’s frustrating and time-consuming to master.

My own learning process has involved, and will continue to involve, lots of searching for expert tips. Stack Overflow and Stack Exchange have been extremely valuable resources.

Over the years, like any good developer, I’ve amassed a collection of code snippets I can turn to often. WordPress Craftsman is my small way of giving back to the open-source community, by sharing some of the tricks I’ve learned. If you’ve landed here via search, welcome.

So, far, I’ve been concentrating on topics that aren’t often discussed elsewhere, and included only snippets in my toolbox that are unique. In the future I may post more entry-level tutorials, but for now my audience is coders who’ve already got wet feet. Most of my posts assume you have reasonable PHP knowledge. Use these code excerpts at your own risk; these worked for me at least once, but may conflict with your particular package. And, let’s assume these require a minimum WP 3.5 (if I become aware of any code sample obsoleted in future versions I’ll update or remove them).

Syntax notes: As the years have gone by, my coding habits have changed. So sometimes you’ll see blocks of code that are more readable, with nice formatting and spacing; others you’ll see — let’s call it ‘rush’ syntax.

One major change has been switching to (and highly preferring) PHP ‘alternative syntax’ instead of ‘curly braces,’ wherever possible. I think it makes the code much more readable. This means you’ll see things like

if ($x > $y):

in some samples, but the equivalent

if ($x > $y) {

in older ones. Sorry if this causes any confusion.

Dave Bushnell