i am unable to create two tables on plugin activation.
Here is what i have tried so far.
function bohio_table() {
global $wpdb;
$wpdb->hide_errors();
// Require upgrade
// Set charset
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}
$bohio = $wpdb->prefix . 'bohio';
$cadence = $wpdb->prefix . 'cadence';
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$queries = array();
// bohio table
array_push($queries, "CREATE TABLE `{$bohio}` (
primary_key mediumint(11) NOT NULL AUTO_INCREMENT,
square_feet varchar(80) NOT NULL,
bed varchar(80) NOT NULL,
bath varchar(80) NOT NULL,
bh_service varchar(80) NOT NULL,
price varchar(80) NOT NULL,
PRIMARY KEY (primary_key)
) {$collate}");
// cadence table
array_push($queries, "CREATE TABLE `{$cadence}` (
id int(11) NOT NULL AUTO_INCREMENT,
fk varchar(80) NOT NULL,
cadence varchar(80) NOT NULL,
price varchar(80) NOT NULL,
PRIMARY KEY (id)
) {$collate}");
foreach ($queries as $key => $sql) {
dbDelta( $sql );
}
}
register_activation_hook( __FILE__, 'bohio_table' );
I have no idea where i am doing wrong?
Calling that in the main file of my plugin as
if( !defined('ABSPATH') ) {
die('You cannot be here');
}
if( !class_exists('BohioClean') ) {
class BohioClean{
public function __construct() {
define('MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('MY_PLUGIN_URL', plugin_dir_url( __FILE__ ));
}
public function initialize() {
// Load tables on the fly
include_once MY_PLUGIN_PATH . 'tables/bohio_table.php';
}
}
$BohioClean = new BohioClean;
$BohioClean->initialize();
}
The file is located under the tables directory and not sure why the tables are not being created on plugin activation. am i missing anything here? Please assist. thanks and kind regards
CodePudding user response:
Although I am not sure, I can say the following.
__FILE__
is probably not working during activation because it resides in a different file.
https://developer.wordpress.org/reference/functions/register_activation_hook/
The first parameter should take the path of the plugin file. You are passing the path to the tables folder. I would recommend defining your hook outside of any class or function in your main plugin file. This hook should work before the plugin is activated. If you still want to define it in the class, either pass the path from your main file or make your own edits to create the path. I wouldn't recommend creating it manually directly.
It would be best to do it this way:
if( !defined('ABSPATH') ) {
die('You cannot be here');
}
if( !class_exists('BohioClean') ) {
class BohioClean{
public function __construct() {
define('MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('MY_PLUGIN_URL', plugin_dir_url( __FILE__ ));
}
public function initialize() {
// Load tables on the fly
include_once MY_PLUGIN_PATH . 'tables/bohio_table.php';
}
}
$BohioClean = new BohioClean;
$BohioClean->initialize();
register_activation_hook( __FILE__, 'bohio_table' );
}
OR
if( !defined('ABSPATH') ) {
die('You cannot be here');
}
if( !class_exists('BohioClean') ) {
class BohioClean{
public function __construct() {
define('MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('MY_PLUGIN_URL', plugin_dir_url( __FILE__ ));
}
public static function initialize() {
include_once MY_PLUGIN_PATH . 'tables/bohio_table.php';
bohio_table();
}
}
$BohioClean = new BohioClean;
register_activation_hook( __FILE__, 'BohioClean::initialize' );
}
OR
...
register_activation_hook( MY_PLUGIN_FILE, 'bohio_table' );
if( !defined('ABSPATH') ) {
die('You cannot be here');
}
if( !class_exists('BohioClean') ) {
class BohioClean{
public function __construct() {
define('MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('MY_PLUGIN_URL', plugin_dir_url( __FILE__ ));
define('MY_PLUGIN_FILE', __FILE__);
}
public function initialize() {
// Load tables on the fly
include_once MY_PLUGIN_PATH . 'tables/bohio_table.php';
}
}
$BohioClean = new BohioClean;
$BohioClean->initialize();
}
I think anyone should fix their problem. You should set up the plugin structure well at the beginning.
//EDIT
This is because, as I mentioned, the __FILE__
variable specifies the path of the file in which it is located. You call it in a file in a subfolder and run the hook like this. What you need to do is add the path from the main plugin file to the hook function. It will work correctly as the __FILE__
variable in the main file will point to the path of your main plugin file.
Here is an example for you to understand better:
In bohio_table.php file:
__FILE__ : .../wp-content/plugins/my-plugin/tables/bohio_table.php
In my-plugin.php file:
__FILE__: .../wp-content/plugins/my-plugin/my-plugin.php
The register_activation_hook need for the path to your main plugin file. Not the file path in any subfolder.