gpt4 book ai didi

php - WORDPRESS UPDATE DB 方法未被调用

转载 作者:行者123 更新时间:2023-11-30 23:23:38 24 4
gpt4 key购买 nike

我正在制作一个 wordpress 插件,我在主 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');
}
?>

在我的自定义类(class)中我有:

<?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() 方法是我添加的新方法,它试图更新数据库以获得额外的功能。但是当插件激活时它永远不会运行。

最佳答案

您只想执行该查询。

global $wpdb;
$wpdb->query($sql);

dbDelta 用于创建新表或更新现有表以符合更新的方案。简而言之,它接受一个 CREATE TABLE 查询,并在必要时将其转换为一个 ALTER TABLE 查询。 The Codex说:

The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary, so it can be very handy for updates [...] Note that the dbDelta function is rather picky, however.

关于php - WORDPRESS UPDATE DB 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com