Wordpress在本地创建插件表,而不是在生产中

时间:2016-05-25 21:32:19

标签: wordpress wordpress-plugin

我正在为客户端制作一个插件,我正在尝试将它安装在他们的测试服务器上。

当我在本地安装插件时,一切正常。但是,当我在测试服务器上安装它时,不会创建表。他们没有打开调试,我没有收到任何安装错误的消息。我使用名为数据库浏览器的插件验证是否未创建表。

这是创建表的代码,其他一切都被删除了。使用此代码,表在本地创建:

<?php
/*
Plugin Name: Integration Rating
*/

class IntegrationRating {

    const INTEGRATIONS_TABLE_NAME = "ir_integrations";
    const RATINGS_TABLE_NAME = "ir_ratings";

    /**
     * Called when the plugin is activated. Creates the tables for the plugin.
     */
    public static function activate_plugin() {

        global $wpdb;

        $sqlCreateTable1 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . IntegrationRating::INTEGRATIONS_TABLE_NAME . "` (
            `post_id` bigint(40) unsigned NOT NULL,
            `rating` int(11) NOT NULL,
            `num_raters` int(11) NOT NULL,
            PRIMARY KEY (`post_id`),
            CONSTRAINT `post_id` FOREIGN KEY (`post_id`) REFERENCES `" . $wpdb->prefix . "posts` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

        $sqlCreateTable2 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . IntegrationRating::RATINGS_TABLE_NAME . "` (
            `post_id` bigint(40) unsigned NOT NULL,
            `rater_email` varchar(255) NOT NULL DEFAULT '',
            `rating` int(1) NOT NULL,
            PRIMARY KEY (`post_id`,`rater_email`),
            CONSTRAINT `ratings_post_id` FOREIGN KEY (`post_id`) REFERENCES `" . $wpdb->prefix . IntegrationRating::INTEGRATIONS_TABLE_NAME . "` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

        $wpdb->query($sqlCreateTable1);
        $wpdb->query($sqlCreateTable2);
    }

    /**
     * Called when the plugin is uninstalled. Removes the plugins tables from the database.
     */
    public static function uninstall_plugin() {

        global $wpdb;

        $sqlDropTable1 = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . IntegrationRating::INTEGRATIONS_TABLE_NAME;
        $sqlDropTable2 = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . IntegrationRating::RATINGS_TABLE_NAME;

        // Delete ratings first because of constraints
        $wpdb->query($sqlDropTable2);
        $wpdb->query($sqlDropTable1);
    }

}

function ir_activate_plugin() {
    IntegrationRating::activate_plugin();
}

function ir_uninstall_plugin() {
    IntegrationRating::uninstall_plugin();
}

register_activation_hook(__FILE__, 'ir_activate_plugin');
register_uninstall_hook(__FILE__, 'ir_uninstall_plugin');

new IntegrationRating();

我希望有人能找到可能出错的线索。

3 个答案:

答案 0 :(得分:3)

我遇到了同样的错误,但我的客户端打开了调试日志。

问题是REFERENCES未授予当前用户。

授予REFERENCES权限然后重新安装插件可以解决您的问题。

答案 1 :(得分:1)

更改你的mysql ENGINE = InnoDB到ENGINE = MyISAM

当您想要显示调试并在创建表时查找实际问题时,您的插件已经激活。在$ wpdb全局声明之后,首先在函数activate_plugin中启用调试。 https://codex.wordpress.org/Class_Reference/wpdb#Show_and_Hide_SQL_Errors

public static function activate_plugin() {

        global $wpdb;
        $wpdb->show_errors();
        ........

使用wordpress hook init 创建新功能。

add_action( 'init', 'plugin_table_installed' ); // Debugging complete remove this

function plugin_table_installed(){
   IntegrationRating::activate_plugin();
}

答案 2 :(得分:0)

请检查后更换以下功能吗?

public static function activate_plugin() {

        global $wpdb;

        $sqlCreateTable1 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . IntegrationRating::INTEGRATIONS_TABLE_NAME . "` (
            `post_id` bigint(40) unsigned NOT NULL,
            `rating` int(11) NOT NULL,
            `num_raters` int(11) NOT NULL,
            PRIMARY KEY (`post_id`),
            CONSTRAINT `post_id` FOREIGN KEY (`post_id`) REFERENCES `" . $wpdb->prefix . "posts` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

        $sqlCreateTable2 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . IntegrationRating::RATINGS_TABLE_NAME . "` (
            `post_id` bigint(40) unsigned NOT NULL,
            `rater_email` varchar(255) NOT NULL DEFAULT '',
            `rating` int(1) NOT NULL,
            PRIMARY KEY (`post_id`,`rater_email`),
            CONSTRAINT `ratings_post_id` FOREIGN KEY (`post_id`) REFERENCES `" . $wpdb->prefix . IntegrationRating::INTEGRATIONS_TABLE_NAME . "` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

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