• 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)
  • 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.ww.wp.xz.cn/Creating_Tables_with_Plugins

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    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

    (@timhu)

    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.

    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.