0

I am creating oop base plugin. So how to create table during plugin installation in side a class.But I could not create table. This is my approach.What wrong with me.

class Notification  
{
    function __construct() {
        register_activation_hook( __FILE__, array($this,'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 );

    }


}

$obj=new notification;

1 Answer 1

0

You should run your register_activation_hook after creating object.

class Notification  
    {
        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 );

        }


    }

    $obj=new Notification();
    register_activation_hook( __FILE__, array($obj,'jal_install') );

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.