Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress plugin won't create custom tables

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #168977
    gilgimech
    Participant

    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 . " (
                    <code>id</code> int(10) unsigned NOT NULL AUTO_INCREMENT,
                    <code>page_no</code> int(11) DEFAULT NULL,
                    <code>language</code> varchar(30) NOT NULL,
                    <code>position</code> int(11) DEFAULT NULL,
                    <code>gdomain</code> varchar(100) DEFAULT NULL,
                    <code>keyword</code> varchar(500) DEFAULT NULL,
                    <code>synonym</code> varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
                    <code>keyword_id</code> int(11) DEFAULT NULL,
                    <code>type</code> enum('heading','description') DEFAULT NULL,
                    <code>created</code> datetime DEFAULT NULL,
                    <code>updated</code> datetime DEFAULT NULL,
                    PRIMARY KEY (<code>id</code>)
                );";
                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.

    id is supposed to be backticks. I can’t get them to show up in block code here.

    #168981
    gilgimech
    Participant

    Fixed the issue. An unrelated call to a different database was the problem

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.