2

I want to add a new table to the Wordpress database. I need this one for a specific section of my Wordpress website, that requires something a bit different than what I can achieve through custom posts.

What would be the correct approach to do that? Is it enough to just add a table of my own or is there something I need to do in PHP code as well to make Wordpress aware of it?

Will this affect in any way Wordpress upgrade procedures?

2 Answers 2

2

Firstly I would try to avoid the use of a custom table using custom post types, custom taxonomies and post/user/term meta. This will greatly increase compatibility and give you a large chunk of free functionality, such as UIs, import/export support, archives, REST APIs, etc

But assuming you cannot use that, for whatever reason, you should:

  • Use the database prefix in your table names where possible. Aside from consistency, some people use a single database for multiple installations, and this scenario will fail if you always use the same table name
  • Use dbDelta to create and update your table. The syntax and input of this function is extremely strict so be careful to follow the instructions to the letter. It's tempting to do it via a wpdb query but don't!
  • Use a wpdb object to make your queries. There's the global wpdb object, but you can create your own if you need to use a different user/pass or connect to a remote server. A lot of people will use mysql_connect etc, this is bad
  • If for whatever reason dbDelta and wpdb are not going to cut it ( rare ), use PDO/mysqli or an ORM library. That this is necessary is very unlikely and improbable.
  • WordPress does not need to be told about your table, and does not keep track of it, that's entirely up to you.
  • Updates of WordPress will not change your tables, but changes to Core tables will be undone on update if the database schema changes.
  • If you're trying to add a new form of meta ( e.g. term meta ), there are already expected table names that map into the meta APIs that you can use

For more information see the codex

1

Conventional wisdom is that you should avoid creating tables and instead use the existing "meta" and "option" tables, as well as custom post types and taxonomies. There are some grounds for that argument. Back several years ago custom tables were more common, and they made a bit of a mess as plugins tended to never clean them up. Using the Core tables and other functionality-- CPT's, etc.-- greatly decreases this issue. On the other hand, while I use them extensively custom post types (as an example) are not a fit for all data. Some things just don't squeeze into that box, or into any other box the Core provides.

  1. WordPress will leave your table alone in general and during updates.
  2. You do not need to make WordPress "aware" of the table, per se, but you will need to write any SQL that deals with the table whether your are querying via $wpdb-> methods are hooking into WP_Query hooks like posts_where.
  3. WordPress provides a function for managing the creation and the updating of the table, but it is maddeningly picky and limited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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