私はワードプレスプラグインを作成しています。メインのphpファイルに関数があります。
<?php
include_once "define.php";
include_once 'includes/form_processor.php';
include_once 'includes/email_subscriber.php';
include_once 'includes/options_page.php';
include_once 'includes/widget.php';
//register the plugin installation methods
register_activation_hook(__FILE__, 'install_simple_subscriber_plugin');
register_deactivation_hook(__FILE__, 'uninstall_simple_subscriber_plugin');
//register the plugin url
wp_enqueue_style('simple-email-subscriber-css', plugins_url('/simple_email_subscriber/stylesheets/style.css'));
function install_simple_subscriber_plugin(){
add_action('plugins_loaded', 'setup_plugin_actions');
email_subscriber::install_database();
email_subscriber::update_database();
}
function setup_plugin_actions(){
if(has_action('publish_post')){
$simple_email_subscriber = new email_subscriber();
//add action listener to post publication
add_action('publish_post', array($simple_email_subscriber, 'on_publish_post'));
add_action('publish__future_post', array($simple_email_subscriber, 'on_publish_post'));
}
//add the admin menu pages
$options_page = new options_page();
}
function uninstall_simple_subscriber_plugin(){
remove_action('plugins_loaded', 'setup_plugin_actions');
}
?>
そして、私のカスタムクラスには次のものがあります。
<?php
class email_subscriber{
/*
* Plugin Core Methods
* */
static function install_database() {
//define create table query
$sql = "CREATE TABLE IF NOT EXISTS ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."(
id mediumint(9) NOT NULL AUTO_INCREMENT,
subscribe_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
email VARCHAR(55) NOT NULL,
UNIQUE KEY id (id)
);";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//HERE IS THE NEW BITS ADDED TO THE EXISTING PLUGIN THAT'S NOT GET CALLED
static function update_database(){
$sql=
"DELIMITER $$
CREATE PROCEDURE Alter_Table()
BEGIN
DECLARE _count INT;
SET _count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'email_subscription' AND
COLUMN_NAME = 'subscribe_all');
IF _count = 0 THEN
ALTER TABLE ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."
ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
ADD COLUMN subscribe_category varchar(512) DEFAULT NULL;
END IF;
END $$
DELIMITER ;
CALL Alter_Table();
DROP PROCEDURE Alter_Table;
";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
//add the options for db defination
add_option("ses_db_version", SES_PLUGIN_VERSION);
}
.................and other codes
}
update_database()メソッドは、私が追加した新しいメソッドであり、追加の関数のためにデータベースを更新しようとしています。ただし、プラグインがアクティブ化されたときに実行されることはありません。