Pergunta

I'm working on a voting plugin for my site and I want to create 2 tables: one that stores votes and another that stores voter ips.

In Codex it suggests to use an if statement to see if the table has already been created when installing the plugin but how can I alter the code if I'm creating 2 tables?

This is my if statement in the plugin install function, currently set to check if 1 table already exists.

...

   $table_name1 = $wpdb->prefix . "voters_ip";
   $table_name2 = $wpdb->prefix . "vote_posts";
   $installed_ver = get_option( "postvote_version" );  

   if($wpdb->get_var("show tables like '$table_name'") != $table_name1) { //unsure how to add both tables

      $sql = "CREATE TABLE " . $table_name1 . " (
      id bigint(20) NOT NULL AUTO_INCREMENT,
      vote_post_id bigint(20) NOT NULL,
      voter_ip varchar(100) NOT NULL,
      UNIQUE KEY id (id)
    );";

      $sql = "CREATE TABLE " . $table_name2 . " (
      id bigint(20) NOT NULL AUTO_INCREMENT,
      vote_post_id bigint(20) NOT NULL,
      up int(11) NOT NULL,
      ddown int(11) NOT NULL,
      UNIQUE KEY id (id)
    );";

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

      add_option("postvote_version", $postvote_version);
   }

....

What is the correct way to check if both tables exist?

Foi útil?

Solução

Basic programming techniques you should have learned before building a plugin:

  1. You can concatenate checks with && (and) and || (or).
  2. You can (and should) guard each CREATE query with its own check

SQL syntax you should have looked into before writing queries on your own:

On a related note, please make sure that you delete these tables when the plugin is uninstalled/deleted.

PS: No offense intended, but it does look like you copy pasted without knowing what the code does. Please be aware that by doing this in a plugin you risk other people's installations!

Outras dicas

I only took a cursory glance at your code, but you've at least three issues.

The first is security related:

CREATE TABLE " . $table_name1 . "

You never know what kind of garbage your function might receive, so better write it like so:

CREATE TABLE `" . str_replace('`', '', $table_name1) . "`

The second is SQL related and already highlighted:

CREATE TABLE

Should be:

CREATE TABLE IF NOT EXISTS

The last and most important is PHP syntax related. The second call to:

$sql =

Should be:

$sql .=

Else you'll never create the first table. Ever.

Lastly, as pointed out in a previous message, it's good practice to store the version of your plugin, or of its tables, in an option. This allows to upgrade it more easily when its own options, and its schema, change.

If you're using unique table names, you can assume with a fair amount of confidence that table 2 exists if table 1 exists. So I would only check for the existence of 1 and, if it's there, skip the scripts ... if it's not, run the scripts.

You can also key off an option you set for your plug-in:

  • If the option exists (i.e. get_option('my-voting-version')) then your plug-in is installed and you don't need to run your database scripts.
  • If the option isn't there, run your scripts and set the option so you don't run them twice.

Even if the plug-in is removed, the option will remain so when it's reactivated you won't re-write the tables. Storing your database version and running scripts based on that also gives you the power of managing upgrades. You can then selectively run upgrade scripts based on what table version previously existed for the site.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top