Support » Plugins and Hacks » Create tables on plugin activation

  • For some reason that I can’t figure out the code below is not creating the database table when the plugin is activated. There are no PHP errors and everything else appears to be working fine.

    Can anybody spot anything obvious I have missed.

    Thanks in advance

    – Andy

    Plugin Name: Local Directory
    Description: A directory for displaying local area businesses and resources.
    // Create The Class
    if (!class_exists("local_directory_plugin")) {
    	class local_directory_plugin {
    			var $local_directory_db_version = "1.0";
    			function local_directory_plugin() { //constructor
    			function local_directory_activate() { // Set up the database
    				global $wpdb;
    			    global $local_directory_db_version;
    				$table_name = $wpdb->prefix . "directory_entries";
    					   if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
    						  $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
    							entry_id INT NOT NULL AUTO_INCREMENT ,
    							name VARCHAR(45) NOT NULL ,
    							description TEXT NOT NULL ,
    							address_name_number VARCHAR(45) NOT NULL ,
    							address_street VARCHAR(45) NULL ,
    							address_town VARCHAR(45) NULL ,
    							address_county VARCHAR(45) NULL ,
    							address_postal_code VARCHAR(45) NOT NULL ,
    							phone VARCHAR(45) NOT NULL ,
    							fax VARCHAR(45) NULL ,
    							url VARCHAR(255) NULL ,
    							is_active TINYINT NOT NULL DEFAULT 0 ,
    							owner_id INT NOT NULL ,
    							category_id INT NOT NULL ,
    							PRIMARY KEY  (<code>entry_id</code>) ,
    							KEY fk_directory_entries_directory_categories (category_id ASC)
    						  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    							// Add the options
    							add_option("local_directory_db_version", $local_directory_db_version);
    							add_option('local_directory_entries_per_page', '10');
    			} // end install function
    			function local_directory_deactivate() { // Uninstall Local Directory
    				//delete options
    			} // end install function
    			function print_admin_summary_page() {	//Prints out the admin summary page
    			}//End function print_admin_summary_page()
    			function print_admin_categories_page() {	//Prints out the admin categories page
    			}//End function print_admin_categories_page()
    			function print_admin_settings_page() {	//Prints out the admin settings page
    			}//End function print_admin_settings_page()
      } //End Class local_directory_plugin
    	// Instantiate the object
    	if (class_exists("local_directory_plugin")) {
    		$ld_plugin = new local_directory_plugin();
    		} // End instantiation
    	//Initialise the admin panel
    	if (!function_exists("intialise_admin_panel")) {
    		function intialise_admin_panel() {
    			global $ld_plugin;
    				if (!isset($ld_plugin)) {
    					add_menu_page('Local Directory', 'Directory', 9, basename(__FILE__), array(&$ld_plugin, 'print_admin_summary_page'));
    					add_submenu_page(basename(__FILE__), 'Local Directory Categories', 'Categories', 8, 'categories-page' ,array(&$ld_plugin, 'print_admin_categories_page'));
    					add_submenu_page(basename(__FILE__), 'Local Directory Settings', 'Settings', 8, 'settings-page' ,array(&$ld_plugin, 'print_admin_settings_page'));
    	}	//end local_directory_ap
    }	// end admin panel initialisation  
    // Set up our hooks
      if (isset($ld_plugin)) {
    		register_activation_hook(basename(__FILE__), 'local_directory_activate');
    		register_deactivation_hook(basename(__FILE__), 'local_directory_deactivate');
    		add_action('admin_menu', 'intialise_admin_panel');
  • The topic ‘Create tables on plugin activation’ is closed to new replies.