Alexander Menger
Alexander Menger

Reputation: 105

Wordpress Plugin Development - Creating a new Table in Database

I´m new in developing my own plugins.

In my plugin I need to create a new Table in Database, while installing it or while the first usage. Is there a way to do this?

Thanks!

Upvotes: 0

Views: 686

Answers (1)

Dipak Kumar Pusti
Dipak Kumar Pusti

Reputation: 1723

global $jal_db_version;
$jal_db_version = '1.0';

function jal_install() {
    global $wpdb;
    global $jal_db_version;

    $table_name = $wpdb->prefix . 'liveshoutbox';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT '' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

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

    add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );

Please check this for detailed information.

Creating Tables With Plugins

Hope this one helps :)

Upvotes: 3

Related Questions