0

I’m making a plugin that uses custom tables.

On my local dev site the tables are being created and everything is working, but when i activate the plugin on a live site the tables aren’t being created.

here’s the code I’m using

// activation hook
register_activation_hook( __FILE__, array( 'Gripper_Activate', 'activate' ) );

// activation class
class Gripper_Activate {
    /**
     * Class Constructor
     */
    public function __construct() {
        // Activate plugin when new blog is added
        add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
    }
    /**
     * Fired when the plugin is activated.
     *
     * @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
     */
    public static function activate( $network_wide ) {
        if ( function_exists( 'is_multisite' ) && is_multisite() ) {
            if ( $network_wide  ) {
                // Get all blog ids
                $blog_ids = Gripper_Get_Blog_IDs::get_blog_ids();
                foreach ( $blog_ids as $blog_id ) {
                    switch_to_blog( $blog_id );
                    self::single_activate();
                }
                restore_current_blog();
            } else {
                self::single_activate();
            }
        } else {
            self::single_activate();
        }
    }
    /**
     * Fired when a new site is activated with a WPMU environment
     *
     * @param int $blog_id ID of the new blog
     */
    public function activate_new_site( $blog_id ) {
        if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
            return;
        }
        switch_to_blog( $blog_id );
        self::single_activate();
        restore_current_blog();
    }
    /**
     * Fired for each blog when the plugin is activated
     */
    private static function single_activate() {
        Gripper_WP_Version_Check::activation_check('3.7');
        Gripper_Create_Tables::create_syn_result_table();
    }
}

// Class to create tables
class Gripper_Create_Tables {
    static $gripper_db_version = '1.0.0';
    public static function create_syn_result_table() {
        global $wpdb;
        $table_syn_result = $wpdb->prefix . "rsg_syn_result";
        if($wpdb->get_var("show tables like '$table_syn_result'") != $table_syn_result){
            $sql = "CREATE TABLE " . $table_syn_result . " (
                `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                `page_no` int(11) DEFAULT NULL,
                `language` varchar(30) NOT NULL,
                `position` int(11) DEFAULT NULL,
                `gdomain` varchar(100) DEFAULT NULL,
                `keyword` varchar(500) DEFAULT NULL,
                `synonym` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
                `keyword_id` int(11) DEFAULT NULL,
                `type` enum('heading','description') DEFAULT NULL,
                `created` datetime DEFAULT NULL,
                `updated` datetime DEFAULT NULL,
                PRIMARY KEY (`id`)
            );";
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
            if( !get_option( "gripper_db_version" ) ) {
                add_option( "gripper_db_version", self::$gripper_db_version );
            }
        }
    }
}

The activation hook is working, because the version check is running, but the tables just aren’t being created on the live server.

2
  • can you confirm that db user that you are connecting with has CREATE TABLE privileges? May 1, 2014 at 14:36
  • Fixed the issue. An unrelated call to a different database was the problem.
    – gilgimech
    May 1, 2014 at 15:23

1 Answer 1

1

dbDelta() is picky-- maddeningly picky.

  • You must put each field on its own line in your SQL statement.
  • You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  • You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
  • You must not use any apostrophes or backticks around field names.

https://codex.wordpress.org/Creating_Tables_with_Plugins#Creating_or_Updating_the_Table

You have violated at least one of those rules-- the last one--, and I believe the second one as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.