database - Fatal error: Call to undefined function dbDelta()

I want to create a table during the activation of the plugin, so I used the code as follows:class Database{private $db_

I want to create a table during the activation of the plugin, so I used the code as follows:

class Database
{
    private $db_version = '1.0', $table_prefix;

    public function __construct()
    {
        global $wpdb;
        $this->table_prefix = $wpdb->prefix;
        register_activation_hook( PLUGIN_INDEX_FILE, array($this, 'dbSetup') );
    }

    public function dbSetup()
    {
        $countriesSQL = "CREATE TABLE $this->table_prefix . countries (
            id int(11) UNSIGNED NOT NULL,
            country_code varchar(2) NOT NULL DEFAULT '',
            country_name varchar(100) NOT NULL DEFAULT '',
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $countriesSQL );
    }
}

But during the activation of plugin, it throws the fatal error as,

Fatal error: Call to undefined function dbDelta() in wp-content/plugins/myPlugin/libraries/Database.class.php on line 26

In some forums, some are suggesting to use, require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); before the function call.

It is working. But I just wanted to know, if it is this the right approach? How to use the dbDelta in the correct manner to make it work?

I want to create a table during the activation of the plugin, so I used the code as follows:

class Database
{
    private $db_version = '1.0', $table_prefix;

    public function __construct()
    {
        global $wpdb;
        $this->table_prefix = $wpdb->prefix;
        register_activation_hook( PLUGIN_INDEX_FILE, array($this, 'dbSetup') );
    }

    public function dbSetup()
    {
        $countriesSQL = "CREATE TABLE $this->table_prefix . countries (
            id int(11) UNSIGNED NOT NULL,
            country_code varchar(2) NOT NULL DEFAULT '',
            country_name varchar(100) NOT NULL DEFAULT '',
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $countriesSQL );
    }
}

But during the activation of plugin, it throws the fatal error as,

Fatal error: Call to undefined function dbDelta() in wp-content/plugins/myPlugin/libraries/Database.class.php on line 26

In some forums, some are suggesting to use, require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); before the function call.

It is working. But I just wanted to know, if it is this the right approach? How to use the dbDelta in the correct manner to make it work?

Share Improve this question edited Jun 13, 2019 at 3:04 gvlasov 1055 bronze badges asked Jan 31, 2017 at 18:37 StrangerStranger 2053 silver badges10 bronze badges 1
  • 2 dbDelta is defined in wp-admin/includes/upgrade.php file and used for updates/upgrades so it is not guaranteed that the file is always loaded and you have to require it to make sure. require_once does just fine as it will not load the file more than once.. – Ismail Commented Jan 31, 2017 at 19:20
Add a comment  | 

1 Answer 1

Reset to default 11

You answered your own question. You need to include the upgrade.php file which contains that function. Otherwise won't be loaded on your plugin update and the function won't exist.

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

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

相关推荐

  • database - Fatal error: Call to undefined function dbDelta()

    I want to create a table during the activation of the plugin, so I used the code as follows:class Database{private $db_

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信