Additional tables in WordPress database

2019-03-06 07:23发布

问题:

I am using WordPress and I would like to develop more services for my website. I am thinking of creating new tables inside WordPress database. Will this approach work? I don't want WordPress to delete my tables while updating etc.

These services will be on separate pages, they will not be WordPress plugins. I just want to use the WordPress database to store my tables.

回答1:

As referenced by a WP Tech Ninja (Samuel Wood, aka Otto) in this post on wordpress.org, you can safely add tables to the Wordpress database and it shouldn't be an issue for upgrades or maintenance.

Adding new tables is fine and several plugins do just that. Twitter Tools, for one.

[...]

Upgrading can modify the database, but if you're using some other table entirely, that table won't be touched by WordPress. It only looks at its own tables.



回答2:

Don't do this.

I actually got a chance to speak with Matt Mullenweg a few years ago and he advised to avoid any plugin that adds tables like the plague, for the following reasons:

Use the option table(s) (wp_options or wp_N_options for multisite) to store your data—that's what they are there for. They are basically key/value so you can store pretty much anything you want in there. By doing that, you also get the benefit of being able to using the Wordpress api's simple calls to read and write your data, so you don't have to mess with writing data handlers.

Automatic cleanup when your plugin is removed is much easier as well, as you can—again—use the API and those functions are handled for you (yes, there are hooks to tell when this happens for this very reason). Removing tables manually is a pain and is difficult to do when your plugin gets uninstalled.