Title: extending wordpress database tables?
Last modified: August 18, 2016

---

# extending wordpress database tables?

 *  [timhu](https://wordpress.org/support/users/timhu/)
 * (@timhu)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/)
 * i am trying to write a plugin that requires storing some meta-data associated
   with each post, but i have some questions:
 * (1) Is database the only/best way to store post meta-data?
    (2) Are there standard
   functions to create new tables in WordPress? (wpdb does not seem to do it) or
   does this need to be done manually through database commands or php APIs? (3)
   Can table creation be done when “Activate” is clicked for the plugin at the Plugins
   Management page?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/#post-561066)
 * 1. yes
    2. yes, and no 3. yes.
 *     ```
       // Install the necessary table.
       function plugin_install() {
       		global $wpdb;
       		$plugin_table_name = $wpdb->prefix . "plugin";
       		if($wpdb->get_var("show tables like '$plugin_table_name'") != $plugin_table_name) {
       ```
   
 *     ```
       $sql = "
       CREATE TABLE " . $plugin_table_name . " (
       id int(11) unsigned NOT NULL AUTO_INCREMENT,
       blahblah varchar(64) NOT NULL DEFAULT '',
       PRIMARY KEY (id)
       );";
       require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
       		dbDelta($sql);
       		}
       ```
   
 * `add_action('activate_wp-plugin.php', 'plugin_install');`
 * Thats all covered in the codex.
    [http://codex.wordpress.org/Creating_Tables_with_Plugins](http://codex.wordpress.org/Creating_Tables_with_Plugins)
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/#post-561073)
 * Note that to store meta-data which is associated with a post, creating a new 
   table is not necessary nor desirable. You should use custom fields instead.
 * add_post_meta(), get_post_meta(), delete_post_meta(), update_post_meta()… those
   sort of functions. They allow you to associate key/value pairs with posts for,
   well, whatever you want. I recommend using keys beginning with underscores (like
   _pluginname_keyname) so that others don’t accidentally use the same names.
 *  Thread Starter [timhu](https://wordpress.org/support/users/timhu/)
 * (@timhu)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/#post-561179)
 * Otto42: thank you very much for these pointers!
 * i originally thought the post_meta table is not for external developer use … 
   good news that there’s no need to create a new table.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/#post-561200)
 * lol, whatever. your welcome anyway.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘extending wordpress database tables?’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [extend](https://wordpress.org/support/topic-tag/extend/)
 * [table](https://wordpress.org/support/topic-tag/table/)

 * 4 replies
 * 3 participants
 * Last reply from: [whooami](https://wordpress.org/support/users/whooami/)
 * Last activity: [19 years, 1 month ago](https://wordpress.org/support/topic/extending-wordpress-database-tables/#post-561200)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
