Building a Useful WordPress Code Library | @thetorquemag
“All The Word That's Fit To Press.”
A WP Engine

Building a Useful WordPress Code Library

Something I have noticed when I am developing WordPress sites is that I very often find myself coding up the same Loop patterns again and again.

How many times have you developed a magazine style layout with a grid of posts, each with a post thumbnail, title, author, date and small excerpt? I know I have – countless times. It’s like I’ve got my own personal “loop” going on myself.

It might look something like this:

Your typical grid layout

Your typical grid layout

And the code behind it might look like this:

I’ll get back to that in a minute.

Not too long ago, Dan Ciederholm released his Pears WordPress theme. Pears is a pattern library for HTML and CSS. It allows you to build customizable patterns and HTML modules, such as forms or navigation, and browse through them.

On each page, the pattern is displayed at the top, and the HTML and CSS are displayed underneath. It is a clever way to create reusable markup patterns that you may use of on virtually every project you do.

Pears: The Pattern Library built with WordPress

Pears: The Pattern Library built with WordPress

Since then, there have been numerous examples of similar CSS pattern libraries such as KSS, but Pears has really stuck in mind. As it’s hosted on a WordPress install, I thought it might be an interesting thing to fork and extend and incorporate into the WordPress loop.

The Big Idea

So here’s my idea: Build a pattern library that allows users to create their own resuable patterns within a WordPress loop (for instance, a grid of posts with thumbnails, title, date and excerpt).

It is essentially a fork of Pears, and laid out in a similar way, but has a few more options. I have built a very simple prototype, using the example pattern at the top.

Looplet: The Pattern Library built for WordPress

Looplet: The Pattern Library built for WordPress

Here’s how it works: A custom post type is created, and filled with dummy content. Each post page, and pattern, pulls in content from this post type in order to build the Loop, which is called using WP_Query.

The different parameters of this loop are customizable through custom fields on the post (see below). Then, custom PHP and HTML can be entered into one metabox on each post, and CSS can be entered into another.

The Loop can be customized using meta boxes in each post.

The Loop can be customized using meta boxes in each post.

My big question at this point is, is this something that you, the WordPress community, would find useful? If it is, there is much work to be done.

The Problems

My PHP skills are not exactly stellar, and already I have run into quite a few setbacks.

Because the WordPress loop is so complex, the proposed project presents a slew of problems. For one, a custom WP_Query offers many different parameters, and it may be foolish to consider including all of them in a separate meta box on each post page. Another problem is that often code has to go before and after the Loop.

My current solution has been to add more custom fields, but this gets unwieldy very quick.

To make this fully customizable requires many custom fields, and it can get a bit confusing and maybe too robust.

To make this fully customizable requires many custom fields, and it can get a bit confusing and maybe too robust.

Also, because the Loop is pulled in from a Custom Post Type, as to avoid pulling in the pattern posts that are created in the main query, there are severe limitations on what parameters can be used.

For instance, Custom Post Types don’t support custom taxonomies. That’s kind of a big deal, when a lot of custom patterns would most likely want to include categories and other tags.

There is also a slight security concern. PHP data is entered into a custom field on the backend, opening a gateway for malicious attack. This may not be the biggest problem, since this is mostly for developers working locally or own on their own development environment, but it is still a concern.

Error handling is also a problem. Because the code is not contained, if there is an error in the custom code, the page itself sometimes won’t show, and it can be hard to diagnose what the problem is, and if its coming from your code of the pattern, or conflicting code in the theme.


So, I’ve come to you, the community, with several questions.

  • Do you find this useful? Is this something you would use?
  • Can you envision a better way to accomplish a pattern library?
  • Are there any problems with this project I have not listed?
  • Have you had experience with any of the problems above, and if so, how have you handled them?
  • And perhaps, most importantly, is this a worthwhile pursuit?

You can view the prototype at:

I put the code up on GitHub, but right now it is a bit in disarray, but you are welcome to take a look:

Feel free to email me at me [at], or find me on Twitter, or leave your thoughts in the comments. It’d be great to hear feedback, suggestions and of course, I would love some help if there any developers out there that think this is a project that is worth a go.

My feeling is that in order for this to be useful, it has to be far more robust, and in turn, may not be all that useful if it is complex. It may be one of those projects that started with a good idea, but isn’t scalable. But, that’s why I’m asking, so please let me know.

More WordPress News From Torque:

There are 12 comments

Your email address will not be published. Required fields are marked *