最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Issue running db create table query from static method

programmeradmin1浏览0评论

I suspect the way I call the table creation function below is the culprit for no table creation (nothing shows in the debug log or query array). Can anyone see my mistake?

// main plugin file, frequentVisitorCoupons.php
require 'vendor/autoload.php';

// the first argument points to this file because I think
// autoload automatically loads the Utilities class here
register_activation_hook(plugin_dir_url(__FILE__) . 'frequentVisitorCoupons.php',
 'Utilities::createTablesIfNotExists');


// classes/utilities.php
<?php    
class Utilities {
  public static function createTablesIfNotExists() {
    global $wpdb;

    $createCouponTableQuery = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}frequentVisitorCoupons_coupons (
    couponId MEDIUMINT NOT NULL AUTO_INCREMENT UNIQUE,
    PRIMARY KEY  (couponId),
    totalHits MEDIUMINT NOT NULL,
    isText BOOLEAN NOT NULL,
    imageUrl TEXT(1000)
    )";

    <2 more table create queries removed>

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($createCouponTableQuery);

    var_dump($wpdb->queries);
    echo <<<'EOD'
    =====$wpdb->queries=====
EOD;
  }
}

I suspect the way I call the table creation function below is the culprit for no table creation (nothing shows in the debug log or query array). Can anyone see my mistake?

// main plugin file, frequentVisitorCoupons.php
require 'vendor/autoload.php';

// the first argument points to this file because I think
// autoload automatically loads the Utilities class here
register_activation_hook(plugin_dir_url(__FILE__) . 'frequentVisitorCoupons.php',
 'Utilities::createTablesIfNotExists');


// classes/utilities.php
<?php    
class Utilities {
  public static function createTablesIfNotExists() {
    global $wpdb;

    $createCouponTableQuery = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}frequentVisitorCoupons_coupons (
    couponId MEDIUMINT NOT NULL AUTO_INCREMENT UNIQUE,
    PRIMARY KEY  (couponId),
    totalHits MEDIUMINT NOT NULL,
    isText BOOLEAN NOT NULL,
    imageUrl TEXT(1000)
    )";

    <2 more table create queries removed>

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($createCouponTableQuery);

    var_dump($wpdb->queries);
    echo <<<'EOD'
    =====$wpdb->queries=====
EOD;
  }
}
Share Improve this question edited Apr 26, 2019 at 16:34 Sean D asked Apr 26, 2019 at 15:59 Sean DSean D 3878 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

plugin_dir_url() returns the URL directory path for your plugin, so this won't work as expected (the function won't be called):

register_activation_hook(plugin_dir_url(__FILE__) . 'frequentVisitorCoupons.php',
 'Utilities::createTablesIfNotExists');

You should have used plugin_dir_path() which returns the file-system directory path for your plugin (e.g. /var/www/public/<user>/wp-content/plugins/your-plugin/).

But if the code is in the main plugin file, then you could simply use __FILE__:

register_activation_hook(__FILE__, 'Utilities::createTablesIfNotExists');

Sample plugin main file:

<?php
/*
 * Plugin Name: My Plugin
 */

require_once 'path/to/classes/utilities.php';

// Installs the tables on plugin activation.
register_activation_hook( __FILE__, 'Utilities::createTablesIfNotExists' );
发布评论

评论列表(0)

  1. 暂无评论