Add Mysql Database Table Using WordPress Theme or Plugin (Works with multisite)

global $senormedia_jal_db_version;
$senormedia_jal_db_version = '1.0';

function senormedia_jal_install() {
	global $wpdb;
	global $senormedia_jal_db_version;

	$table_name = $wpdb->prefix.'_subscriptions';
	$charset_collate = $wpdb->get_charset_collate();

	$sql = "CREATE TABLE $table_name (
    subscribe_id int(11) NOT NULL auto_increment,
		site_id  varchar(500)  NOT NULL,
    email  varchar(500)  NOT NULL,
    status	varchar(256)	NOT NULL,
    url	varchar(256)	NOT NULL,
    date timestamp NOT NULL default CURRENT_TIMESTAMP,
		PRIMARY KEY  (subscribe_id)
	) $charset_collate;";

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

	add_option( 'senormedia_jal_db_version', $senormedia_jal_db_version );
}

register_activation_hook( __FILE__, 'senormedia_jal_install' );

function myplugin_update_db_check_senormedia() {
    global $senormedia_jal_db_version;
    if ( get_site_option( 'senormedia_jal_db_version' ) != $senormedia_jal_db_version ) {
        senormedia_jal_install();
    }
}
add_action( 'plugins_loaded', 'myplugin_update_db_check_senormedia' );

Leave a Reply

Your email address will not be published. Required fields are marked *

Need Help With Code?