Skip to content

Instantly share code, notes, and snippets.

@dcangulo
Created April 5, 2018 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcangulo/c7eaa651029f30a4951bdaa0843e9c92 to your computer and use it in GitHub Desktop.
Save dcangulo/c7eaa651029f30a4951bdaa0843e9c92 to your computer and use it in GitHub Desktop.
A simple WordPress plugin the creates custom table on plugin activation. Visit https://www.davidangulo.xyz/ for more information.
<?php
/*
Plugin Name: Create database table
Plugin URI: https://www.davidangulo.xyz/portfolio/
Description: A simple WordPress plugin that creates a database table on activation.
Version: 1.0.0
Author: David Angulo
Author URI: https://www.davidangulo.xyz/
License: GPL2
*/
register_activation_hook( __FILE__,"myPluginCreateTable");
function myPluginCreateTable() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "customtable";
$sql = "CREATE TABLE `$table_name` (
`id` int(11) NOT NULL,
`column2` varchar(220) DEFAULT NULL,
`column3` varchar(220) DEFAULT NULL,
`column4` int(11) DEFAULT '1',
PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta($sql);
}
}
//Visit the tutorial for more information
//https://www.davidangulo.xyz/website-development/how-to-create-database-tables-when-your-plugin-is-activated/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment