1

I manually added a table to my wordpress database using sql using CREATE TABLE. I want to add functions for adding, modifying, and removing the data from this table just like with wp_update_user().

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

More about the problem:

I want to have users of the site be able to join groups/teams. I have set up a Teams table (it has team ID, team type, and team name) and a Join table (with user id/team id) in my database. I want to be able to call functions related to these tables from my page_template.php file.

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

closed as too broad by Mark Kaplun, TheDeadMedic, Jacob Peattie, Christine Cooper, fuxia Aug 23 '18 at 23:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You're going to have to do a lot of the lifting and query writing from scratch. Are you absolutely sure a custom taxonomy/meta/post type wouldn't do the job? If so your life would be significantly easier – Tom J Nowell Aug 16 '18 at 23:03
  • @TomJNowell I'm certain that I would like a custom data type – ellen Aug 16 '18 at 23:06
  • 2
    That's unfortunate, you're going to have to construct the UI, all the template loading code, all the caching mechanisms, all the REST endpoints, functions, classes, etc from scratch. WP provides the wpdb class which you can use to make the queries themselves, but you're still going to have to write all the SQL yourself – Tom J Nowell Aug 16 '18 at 23:10
  • this is a software design question which has nothing to do with wordpress development. You should design your code based on your general approach to software design: OOP, functional, etc. DB access is just a non interesting after thought in the big scheme of things, something that do not worth thinking if it even requires an interface before you have actual understanding of all the use cases of it, something you are likely to have only after writing the business logic, not before. – Mark Kaplun Aug 18 '18 at 4:48
  • I really don't understand why you're not just using a custom taxonomy for teams. – TheDeadMedic Aug 18 '18 at 13:54
6

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.

  • Hi Tom, I just want a reference on how to do it properly. I found very little about this on the Wordpress Lessons/Getting started websites. I have 3 years software development experience and 0 worries about writing sql statements to use with dbdelta if required. – ellen Aug 17 '18 at 18:49
  • i will edit the original post to add more information about the problem – ellen Aug 17 '18 at 18:49
  • 2
    @ellen I'm afraid your expectations are higher than what's available. Beyond using wpdb and dbdelta, there really isn't much to talk about, at which point it very quickly goes into non WP general PHP. I really don't know what more you expect without going into the basics of how functions work which you already know. WordPress provides no scaffolding, no supporting interfaces, no structures. It's literally just wpdb and dbdelta, when I say you have to do everything yourself I really mean it, there is no proper way, and no WP way to do it – Tom J Nowell Aug 18 '18 at 3:35
  • @ellen I've updated my answer but I get the feeling that your question can never be concretely answered in a canonical way, that you're mostly looking for best practices and a discussion. I really don't know what else I can say =/ – Tom J Nowell Aug 18 '18 at 3:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.