0

Hi I have a terrible problem. I generated the code plug on the page: http://wppb.me/

And now, in no way do I add a table to the SQL database installation. I tried to do it this way: includes/class-my-activator.php

class My_Activator {
    public static function activate() {

        register_activation_hook( __FILE__,'jal_install' );
    }


    function jal_install()
    {

        global $wpdb;
        global $jal_db_version;
        $table_name = $wpdb->prefix . 'fnotice';
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
                    id INT NOT NULL AUTO_INCREMENT,
                    msg varchar(500) NOT NULL,
                    time DATETIME NOT NULL,
                    type varchar(350) NOT NULL,
                    link varchar(350) NOT NULL,
                    status ENUM('0', '1') NOT NULL default '1',
                    PRIMARY KEY (id) )";

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

        add_option( 'jal_db_version', $jal_db_version );

    }

}
1

1 Answer 1

2

it is better you write like this:

class My_Activator {

    public function jal_install()
    {

        global $wpdb;
        global $jal_db_version;
        $table_name = $wpdb->prefix . 'fnotice';
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
                    id INT NOT NULL AUTO_INCREMENT,
                    msg varchar(500) NOT NULL,
                    time DATETIME NOT NULL,
                    type varchar(350) NOT NULL,
                    link varchar(350) NOT NULL,
                    status ENUM('0', '1') NOT NULL default '1',
                    PRIMARY KEY (id) )";

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

        add_option( 'jal_db_version', $jal_db_version );

    }

}

$myActivator = new My_Activator();
register_activation_hook( __FILE__, array( $myActivator, 'jal_install' ) );
1
  • 1
    Some argumentation why this is better, would help people understand your answer.
    – cjbj
    Jun 18, 2016 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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