Adding custom tables to WordPress

I had something similar recently. The context was a store but the model is fairly typical.

To greatly simplify, there was a table for Products and another for Serial Numbers (distributed when purchase was completed) to be imported or linked.

At first I tried to make Custom Post Types (CPT) fit the model for both. It was fairly easy for Products – I used WooCommerce (Products was the CPT). To get the data into WordPress/WooCommerce I first mucked around with SQL then bought a plugin called WP All Import (plus the WooC addition) that made the process much easier. This plugin has options for importing just about anything into WordPress as an import. Dump your table as XML or CSV, line up the fields and it does the rest.

Advantage of using a custom post type (be it Products or some other type): users can easily “create, read, update and delete any of the rows in the database tables.”

So that would solve your problem, if a CPT worked for all your data. Creating CPTs is easy, and there are many great plugins that make it a lot easier still. You could then import your data using the plugin above (or if you’re a SQL wizard, try that!)

However, I tried the same with my Serial Numbers table – no cigar. In short, it just wasn’t a “Post”. They’re almost like metadata. So then I tried bringing them into WordPress and metadata for Products. But it was just too complex. The Serials needed to be assigned to Products and Orders when purchased. Also, having them in a discrete data set, makes it much easier to manage them if something needs manual or bulk fixing.

So, for the serial numbers table, I created a custom table in WordPress and used that. This works fine. I did it all manually and it was (a) a great learning experience and (b) a complete chore. If I was going to do it again, I’d use a plugin and spend the time doing other things. There are many plugins that can help. If you want to kill two birds with one stone, look at Pods – it can create CPTs and it can create custom tables for you. I’m pretty sure it allows you to allow your users to “create, read, update and delete any of the rows in the database tables” too – so it might be the fastest easiest route.

If you’re going to go the “manually create table” route and build an interface for your users, here are a few starting points:

Creation: http://www.paulund.co.uk/creating-custom-tables-wordpress-plugin-activation

Working with data: http://www.makeuseof.com/tag/working-custom-database-tables-wordpress/