I've created a simple Class for my wordpress plugin. The main file looks like this:
<?php
/*
* Plugin Name: Codes
* Description: Generates a coupon code after checkout
* Version: 0.1.0
* Author: XXX
*/
require_once('inc/DatabaseUtils.php');
$codes = new Codes();
class Codes {
private $databaseUtils;
/**
* Create a new instance
*
* @param null
* @return void
*/
function __construct() {
$this->regsiter_hooks();
$this->databaseUtils = new DatabaseUtils();
}
/**
* Called on plugin activation
*
* @param null
* @return void
*/
function activate() {
$this->databaseUtils->setup_codes_table();
}
/**
* Called on plugin deactivation
*
* @param null
* @return void
*/
function deactivate() {
}
/**
* Register plugin hooks
*
* @param null
* @return void
*/
function regsiter_hooks() {
register_activation_hook(__File__, array($this, 'activate'));
register_deactivation_hook(__File__, array($this, 'deactivate'));
}
}
In the folder ´inc´ I have a DatabaseUtils.php file. This is a class with a function to create a table in the wordpress database after activating the plugin. Here the content:
<?php
class DatabaseUtils {
function setup_codes_table() {
global $wpdb;
global $charset_collate;
$table_name = $wpdb->prefix . 'pin';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`pincode` bignit(128) DEFAULT NOT NULL,
PRIMARY KEY (`id`)
)$charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
If you see, I call the function in the activation method in my main file. But when I activate the plugin there is no new table in my database.
Does anyone have a idea why? Is there a way to debug the register_activation_hook
?
Thank's for your help!
$bar = dbDelta( $sql ); var_dump($bar);
that should at least get you an error message ifdbDelta
fails.