我正在开发一个个人投票插件,我不打算公开发布它,因为我还在学习WP。
一般来说,你可以投票给帖子“向上”或“向下”。我有两个表,一个收集IP(不允许多次投票),另一个名为“post\\u votes”的表收集每个帖子的投票数。
当我发布一篇新帖子时,它的ID存储在“post\\u votes”中,我可以在人们投票时更新每个帖子的“up”、“down”。这在几天前奏效了。我做了一些修改,但我不明白为什么它不再工作了。。
这是目前为止的全部代码。。我仍然需要实施一些安全预防措施。
<?php
/*
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\');
dbDelta($sql);
dbDelta($sql2);
add_option("vote_posts_version", $vote_posts_version);
}
}
register_activation_hook(__FILE__,\'vote_posts_install\');
//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
?>
它成功创建了这两个表,但没有在操作中插入post ID