plugins - Issue running db create table query from static method

I suspect the way I call the table creation function below is the culprit for no table creation (nothing shows in the de

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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745548657a4632460.html

相关推荐

  • plugins - Issue running db create table query from static method

    I suspect the way I call the table creation function below is the culprit for no table creation (nothing shows in the de

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信