Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a personal voting plugin, I don't intend to release it publicly because I'm still learning WP.

In general, you can vote posts "up" or "down". I have 2 tables, one collects IPs (to disallow voting multiple times) and another table named "post_votes" collects the number of votes for each post.

When I publish a new post its ID is stored in "post_votes" and I can update the "up", "down" for each post when people vote. This worked a few days ago. I did some modifications and I can't figure out why it's not working anymore..

This is the entire code so far.. I still need to implement some security precautions.

Plugin Name: Vote Posts

global $vote_posts_version;
$vote_posts_version = "1.0";

function vote_posts_install () {

   global $wpdb;
   global $vote_posts_version;

   $table_1 = $wpdb->prefix . "voter_ips";
   $table_2 = $wpdb->prefix . "vote_posts"; 

   if($wpdb->get_var("SHOW TABLES LIKE '$table_1 AND $table_2'") != $table_1 && $table_2) {

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

       $sql2 = "CREATE TABLE " . $table_2 . " (
      id bigint(20) NOT NULL AUTO_INCREMENT,
      vote_post_id bigint(20) NOT NULL,
      up int(11) NOT NULL,
      down int(11) NOT NULL,
      UNIQUE KEY id (id)

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

      add_option("vote_posts_version", $vote_posts_version);


//add and delete votes

function add_votes($post_ID)  {
global $wpdb;
   if (!($wpdb->get_row("SELECT vote_post_id FROM wp_vote_posts WHERE vote_post_id = $post_ID" ) ) ) { //if not exists add post ID to table
      $wpdb->insert( $wpdb->prefix . 'vote_posts', array( 'post_id' => $post_ID ) );
      return $post_ID;
add_action ( 'publish_post', 'add_votes' ); //should trigger function on publish post

function delete_votes($post_ID)  {
    global $wpdb;
    $wpdb->query("DELETE FROM wp_vote_posts WHERE vote_post_id = $post_ID");
    return $post_ID;
add_action ( 'delete_post', 'delete_votes' ); //deletes row from post_votes table when a post is deleted from the WP panel


It successfully creates the 2 tables but doesn't insert the post ID on action

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

No offense, but I think I'm agreeing with a commenter from your other question... You really need to read PHP, MySQL, and regular expression tutorials before you get into any of this. More importantly, you need to learn how to search a code base using regular expressions in your favorite editor, and understand the code you're reading.

Just to give you a few examples, this:

SHOW TABLES LIKE '$table_1 AND $table_2'

Is looking for a table called:

$wpdb->prefix . "voter_ips AND " . $wpdb->prefix . "vote_posts"

Bad, bad, bad...

Along the same lines, while your tables both have a not null unique key, which technically amounts to a primary key, the MySQL rewrite engine and optimizer don't treat those in the same way as primary keys, so you should declare it as such instead.

Then, there are the potential security holes:

SELECT vote_post_id FROM wp_vote_posts WHERE vote_post_id = $post_ID

... without casting $post_ID to (int) beforehand...

It also helps to var_dump() stuff if you're not using a PHP debugger. e.g. in your add_votes() call, how about this, to see what's happening?:

share|improve this answer
No offence taken. The code I have is almost identical to the one in WP Codex. Shouldn't the official documentation be correct? I thought the global wpdb variable was implemented with a PHP debugger. Lastly, I don't understand why SHOW TABLES LIKE '$table_1 AND $table_2' is bad. That statement is also in Codex and used on many plugins I studied. Thanks –  at least three characters Nov 20 '10 at 22:07
I do appreciate your answer but you didn't elaborate the first example. This my first plugin attempt, I'm sure you're right –  at least three characters Nov 20 '10 at 22:11
Please reference where that is in codex. –  hakre Nov 21 '10 at 0:29
Here codex.wordpress.org/Creating_Tables_with_Plugins Notice the $table_name = $wpdb->prefix . "liveshoutbox"; if($wpdb->get_var("show tables like '$table_name'") != $table_name) { –  at least three characters Nov 21 '10 at 19:40
SHOW TABLES LIKE '$table_1 AND $table_2' will, literally, look for a table whose name is wp_voter_ips AND wp_vote_posts. What you're looking for is more along the lines of: SHOW TABLES LIKE '$table_1' OR LIKE '$table_2', though I honestly don't know if MySQL's SHOW statement will accept it -- I'm more of a PostgreSQL junkie, myself. –  Denis Nov 21 '10 at 23:59
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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