Reasons for custom tables in WordPress plugins
Pippin Williamson is doing a new series on building a database abstraction layer that’s sure to be really educational. The first post in the series offers reasons for using custom tables in WordPress plugins.
For a long time, the majority of “best practice” advocates for WordPress development screamed against almost all custom table usage. After all, it makes it so much easier to use existing WordPress functionality when you use existing WordPress tables. I was one of these people, because I like doing things the way I know.
The problem, as Pippin points out, is that it doesn’t work so well in a lot of scenarios. He uses the example of eCommerce and explains why storing eCommerce data (like orders) in the posts and post meta tables is a pretty bad idea.
He makes some very good points and at this point in my life I absolutely agree with him, at least in regard to data storage. However, as I’m sure he’ll point out later in this series, it’s still important to help developers use tools — built into the plugin’s API — that mirror WordPress’s familiar functions and classes.
For instance, querying data as an order in the eCommerce example is easy if it’s a custom post type. You just use
WP_Query and you get your data. And of course you have all the other handy post type style functions, template tags, and other WordPress stuff available at this point.
With a custom table, getting a bunch of out of the box functionality is not so easy. However, the plugin developer could do something like
SomePrefix_Order_Query and use similar arguments to
WP_Query, so that someone that knows how to navigate WordPress can navigate the plugin too.
It’s a tricky balance between making plugins easy to manipulate for beginner and intermediate developers versus making scalable, sensibly architected software. One strategy has helped make WordPress ubiquitous, the other has helped make it infamous.
This can get complicated and cumbersome, to map a bunch of WordPress functionality, and I look forward to seeing how Pippin recommends to address these challenges in the future of this series. He’s been on both sides of a balance and I’m sure has a lot of wisdom he can share us.