Why my database table not created on activation?

I try to build an plugin with an custom db table. So I include the notifications.php in the plugin index.php file. If I do a die; in the public function init() (in notifications.php), it dies so it’s loaded, but the register_activation_hook not triggers on activate te plugin and the my_notifications_install fuction not execute. What’s the problem? The activation not respond with an error.

The plugin structure:

/plugins/plugindir/index.php

// Notifications
include_once plugin_dir_path( __FILE__ ) . ‘/notifications.php’;
$my_notifications = new my_notifications( __FILE__ );

/plugins/plugindir/notifications.php

class my_notifications {

public function __construct( ) {
$this->init();
}

public function init() {
global $my_notifications_db_version;
$my_notifications_db_version = ‘1.0’;
register_activation_hook( __FILE__, array( $this, ‘my_notifications_install’ ) );
}

private function my_notifications_install() {
global $wpdb;
global $my_notifications_db_version;

$table_name = $wpdb->prefix . ‘my_notifications’;

$charset_collate = $wpdb->get_charset_collate();

$sql = “CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT ‘0000-00-00 00:00:00’ NOT NULL,
user_id mediumint(9) NOT NULL,
post_id mediumint(9) NOT NULL,
attachment_id mediumint(9) NOT NULL,
message text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;”;

require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql );

add_option( ‘my_notifications_db_version’, $my_notifications_db_version );
}

}

Read more here:: Why my database table not created on activation?

Leave a Reply

Your email address will not be published. Required fields are marked *