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' );

Written by marquin

Web Developer & Designer. I am experienced in all design software and coding languages including HTML, CSS, PHP, OOP, JavaScript, JQuery, JavaScript, XML, WordPress, Magento and other tools for media development.

View all author posts →

Leave a Reply

Your email address will not be published.

View Some Of Our Services