What’s the proper way to use a custom table? [closed]

Should I be creating an interface for my new table? How do I go about that?

Unfortunately, there is no interface to implement, you’re going to have to build everything from scratch. This includes:

  • The admin pages
  • The edit screens
  • The saving and updating and deleting
  • All the classes and functions will need building from scratch
  • All the caching
  • REST API endpoints
  • Frontend archives, templates, pretty URLs/rewrite rules
  • Pagination
  • All the SQL

The only thing WP provides that would be helpful to you here is the $wpdb variable that you can use to make SQL queries.

I want to add functions for adding, modifying, and removing the data from this table just like with wp_update_user().

You’re going to have to build all of these from scratch if you want them. WordPress provides nothing of help here

For instance, I want to call a function that will insert a new team into the database based on a user’s form input.

You’re going to have to build the function from scratch. There is no recommended path for what you’re doing, it’s generic PHP development that just happens to be in a WP site. None of the WP APIs will be of help

I manually added a table to my wordpress database using sql using CREATE TABLE

The dbdelta function can also be used to create the table and update its schema, but it’s a very finnicky and unforgiving function, that expects a CREATE TABLE statements, with extremely specific requirements about formatting, and zero wiggle room. E.g, if you don’t put 2 spaces after the PRIMARY keyword, it won’t work, and plenty of other restrictions.

What’s the proper way to use a custom table?

There isn’t one. The reason you haven’t found what you’re looking for is because it doesn’t exist. WordPress provides little to nothing in terms of support, guidance, infrastructure, interfaces, or help for using custom tables.

Common best practice holds that custom tables are rarely needed, and that in most cases the use of a custom table is a sign that something has gone wrong. Most cases of custom table use are for interacting with tables that already exist somewhere else.

Are there ways you can make life easier for yourself? Yes! But those aren’t WP specific, they’re obvious things like abstractions and wrapper functions that would apply to any PHP program.

For example, once I used the Doctrine ORM library to implement a set of custom tables. The plugin had very little WP code in it, and might as well have been a standalone PHP application. In hindsight I could have done it just as well with custom post types and a non-public taxonomy.

What you should be looking for is general PHP custom table stuff, not WP specific. Outside of wpdb for making the queries, you’re poking at a dead end


As an aside, have you considered just using a custom user taxonomy? Taxonomies aren’t just for posts, taxonomies are for IDs! As long as that ID is numeric and always refers to the same type of thing, it could be posts, comments, users, house numbers, etc Just don’t mix them.