1

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

在我的自定义课程中,我有:

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

4

1 回答 1

0

您只想执行该查询。

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

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

dbDelta 函数检查当前表结构,将其与所需的表结构进行比较,并根据需要添加或修改表,因此它对于更新非常方便 [...] 请注意,dbDelta 函数相当挑剔,但是.

于 2013-01-17T16:16:40.740 回答