0

I read the Codex and some other tutorials describing the process of creating a new table for WordPress.

I thing that the steps are pretty straightforward; however, I'm encountering a small problem.

I am developing a theme (not a plugin) and I am creating some features which require a new WordPress table.

All the tutorials refer to the case when there is a separate plugin involved which must be activated. In that case, the hook looks something like this:

register_activation_hook(__FILE__,'my_plugin_data_tables_install');

The problem is that I don't have any plugin to be activated. What kind of hook should I use to make sure that the table is created correct and only once.

  • Do you really need a new DB table, or could your theme features go into the Options table? See codex.wordpress.org/Options_API for more information. – Pat J Apr 16 '13 at 18:44
  • You could look into after_switch_theme, but it is rare that a theme should require special tables. – vancoder Apr 16 '13 at 18:49
  • @PatJ I am creating a Slider inside the theme (it is not separated from the theme because I intend it to be the "Default Slider"). This Slides are quite different from a standard post and I thought that creating a new table for them would be a good decision. – Ionut Apr 16 '13 at 19:02
  • @vancoder I am creating a Slider inside the theme (it is not separated from the theme because I intend it to be the "Default Slider"). This Slides are quite different from a standard post and I thought that creating a new table for them would be a good decision. – Ionut Apr 16 '13 at 19:02
  • 2
    I would recommend using Meta Boxes. Have a look at the Custom Metabox Library if you haven't already -- it makes handling metabox stuff a lot easier, IMHO. – Pat J Apr 16 '13 at 19:47
3

As mentioned, after_switch_theme should be a pretty good choice. Your table create/update function will only run when you switch themes, which should be roughly like activating a plugin. If you use dbDelta to create and/or update the table there should be no issue with table creation. From the Codex:

global $wpdb;
$installed_ver = get_option( "jal_db_version" );

    if( $installed_ver != $jal_db_version ) {

      $sql = "CREATE TABLE $table_name (
         id mediumint(9) NOT NULL AUTO_INCREMENT,
         time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
         name tinytext NOT NULL,
         text text NOT NULL,
         url VARCHAR(100) DEFAULT '' NOT NULL,
         UNIQUE KEY id (id)
      );";

      require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
      dbDelta( $sql );

      update_option( "jal_db_version", $jal_db_version );
    }

That will check the hard-coded table version against the version stored in the database and create or update if necessary. Note: it will create or update based on calculations made by dbDelta. It will not try to create the same table twice. If the table already exists it will try to apply the differences between the existing table and the new table definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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