2016-05-25 30 views
-3

我正在爲客戶端製作一個插件,我正在嘗試將它安裝在他們的測試服務器上。Wordpress在本地創建插件表,不在生產

當我在本地安裝插件時,一切正常。但是,當我將它安裝在測試服務器上時,表格不會被創建。他們沒有打開調試,並且我沒有收到任何安裝錯誤的消息。我通過使用一個名爲數據庫瀏覽器的插件來驗證這些表沒有被創建。

這是創建表的代碼,其他所有內容都被刪除。有了這個代碼表格創建本地:

<?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(); 

我希望有人有一些線索可能是錯的。

+0

猜測,可能是您正在使用的數據庫用戶沒有足夠的權限來更改數據庫架構。 – Aditya

回答

3

我遇到了同樣的錯誤,但我的客戶端打開了調試日誌。

而問題是REFERENCES未被授予當前用戶。

格蘭特REFERENCES特權然後重新安裝插件可以解決您的問題。

1

變化在你的mysql ENGINE = InnoDB的發動機= 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鉤初始化後。

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

function plugin_table_installed(){ 
    IntegrationRating::activate_plugin(); 
} 
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); 
    } 
相關問題