我正在制作一个使用自定义表的插件。
在我的本地开发站点上,正在创建表,一切都正常,但当我在一个活动站点上激活插件时,表不会被创建。
这是我使用的代码
// activation hook
register_activation_hook( __FILE__, array( \'Gripper_Activate\', \'activate\' ) );
// activation class
class Gripper_Activate {
/**
* Class Constructor
*/
public function __construct() {
// Activate plugin when new blog is added
add_action( \'wpmu_new_blog\', array( $this, \'activate_new_site\' ) );
}
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
public static function activate( $network_wide ) {
if ( function_exists( \'is_multisite\' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids
$blog_ids = Gripper_Get_Blog_IDs::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::single_activate();
}
restore_current_blog();
} else {
self::single_activate();
}
} else {
self::single_activate();
}
}
/**
* Fired when a new site is activated with a WPMU environment
*
* @param int $blog_id ID of the new blog
*/
public function activate_new_site( $blog_id ) {
if ( 1 !== did_action( \'wpmu_new_blog\' ) ) {
return;
}
switch_to_blog( $blog_id );
self::single_activate();
restore_current_blog();
}
/**
* Fired for each blog when the plugin is activated
*/
private static function single_activate() {
Gripper_WP_Version_Check::activation_check(\'3.7\');
Gripper_Create_Tables::create_syn_result_table();
}
}
// Class to create tables
class Gripper_Create_Tables {
static $gripper_db_version = \'1.0.0\';
public static function create_syn_result_table() {
global $wpdb;
$table_syn_result = $wpdb->prefix . "rsg_syn_result";
if($wpdb->get_var("show tables like \'$table_syn_result\'") != $table_syn_result){
$sql = "CREATE TABLE " . $table_syn_result . " (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`page_no` int(11) DEFAULT NULL,
`language` varchar(30) NOT NULL,
`position` int(11) DEFAULT NULL,
`gdomain` varchar(100) DEFAULT NULL,
`keyword` varchar(500) DEFAULT NULL,
`synonym` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`keyword_id` int(11) DEFAULT NULL,
`type` enum(\'heading\',\'description\') DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);";
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
if( !get_option( "gripper_db_version" ) ) {
add_option( "gripper_db_version", self::$gripper_db_version );
}
}
}
}
激活挂钩正在工作,因为版本检查正在运行,但没有在实时服务器上创建表。