Wordpress Plugin Development tutorial from scratch (Part 8) Make Database Table on Plugin Activation

Published: 15 November 2017
on channel: Online Web Tutor
15,521
126

In this video session i have explained about creating database table while plugin activation,

#WordpressTutorialsPoint
#WordpressLearnersHub
#WordpressCustomizations
#LearnWordpressStepbyStep
#WordpressBySanjay

Steps to do this task:
#1: Create Sql Query First
#2: Embed generated code to function
#3: Bind with action hook

First we need Sql Query to create table,
===============
CREATE TABLE `wp_custom_plugin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`phone` varchar(150) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Then we have to make a seperate function to run when plugin activates,
// table generating code
function custom_plugin_tables(){
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

if (count($wpdb-get_var('SHOW TABLES LIKE "wp_custom_plugin"')) == 0){

$sql_query_to_create_table = "CREATE TABLE `wp_custom_plugin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`phone` varchar(150) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1"; /// sql query to create table

dbDelta($sql_query_to_create_table);

}
}

and Finally we have to attach with Action hook,
register_activation_hook(__FILE__,'custom_plugin_tables');

SOCIAL :
===============
Subscribe :    / @onlinewebtutor  
FACEBOOK :   / onlinewebtutorhub  
TWITTER:   / owthub  
BLOG: https://onlinewebtutorhub.blogspot.in/

RECOMMENDATION:
===============
1. Some knowledge of PHP, HTML for this video series
2. How to Install Wordpress:    • Wordpress Theme Development tutorial ...  
3. register_activation_hook: https://developer.wordpress.org/refer...

Also you can learn Wordpress Custom
===============
Theme Development:    • Wordpress Theme Development tutorial ...  
Widget Development:    • Wordpress Widget Development tutorial...  

Tags
===============
online web tutor,
profotech solutions,
wordpress plugin development from scratch in hindi,
wordpress plugin development from scratch in easy steps,
wordpress plugin development,
wordpress plugin development in easy steps,
plugin development in wordpress from scratch in hindi,
plugin development in wordpress by online web tutor,
plugin development in wordpress tutorials from scratch in hindi,
plugin theme development tutorials in hindi,
plugin development tutorials in hindi,
owt tuts,

Thanks
Online Web Tutor
Keep learning and Sharing :)

~-~~-~~~-~~-~
Please watch: "Learn backbone.js tutorial from scratch"
   • Learn backbone.js tutorial from scrat...  
~-~~-~~~-~~-~