Create plugin in WordPress in which create table in DB on activate
This blog is for beginner who ant ti learn who wants to learn how to build wordpress plugin. Wordpress allows us to use defaults core functions without creating them from starting. When you are going to create a plugin then you should check there is no error in the code. If something went wrongs in your code in custom plugin then there is might some chances that the website will stopped working.
In this blog I will build a new plugin which will create a new table name`custom_table` in database on plugin activation in which there are 2 columns id and title.
<?php
/*
* Plugin Name: Plugin Creation
* Plugin URI: https://ecode-learn.com
* Description: It will create a table on plugin activation in current database
* Version: 1.0
* Author: Ecode Learn
* Author URI: https://ecode-learn.com
*/
function create_table_on_plugin_activate() { global $wpdb; $tblName = $table_prefix . "custom_table"; // if table 'custom_table' is already created then table will not create if($wpdb->get_var( "show tables like '$tblName') != $tableNAme) { $sql = "CREATE TABLE "'. $tableNAme . "' ( "; $sql .= " 'id' int(11) NOT NULL auto_increment, "; $sql .= " 'title' varchar(255) NOT NULL, "; $sql .= " PRIMARY KEY 'order_id' ('id') "; $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); dbDelta($sql); } } register_activation_hook( __FILE__, 'create_table_on_plugin_activate' );
This blog is for beginner who ant ti learn who wants to learn how to build wordpress plugin. Wordpress allows us to use defaults core functions without creating them from starting. When you are going to create a plugin then you should check there is no error in the code. If something went wrongs in your code in custom plugin then there is might some chances that the website will stopped working.
In this blog I will build a new plugin which will create a new table name`custom_table` in database on plugin activation in which there are 2 columns id and title.
<?php
/*
* Plugin Name: Plugin Creation
* Plugin URI: https://ecode-learn.com
* Description: It will create a table on plugin activation in current database
* Version: 1.0
* Author: Ecode Learn
* Author URI: https://ecode-learn.com
*/
function create_table_on_plugin_activate() { global $wpdb; $tblName = $table_prefix . "custom_table"; // if table 'custom_table' is already created then table will not create if($wpdb->get_var( "show tables like '$tblName') != $tableNAme) { $sql = "CREATE TABLE "'. $tableNAme . "' ( "; $sql .= " 'id' int(11) NOT NULL auto_increment, "; $sql .= " 'title' varchar(255) NOT NULL, "; $sql .= " PRIMARY KEY 'order_id' ('id') "; $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); dbDelta($sql); } } register_activation_hook( __FILE__, 'create_table_on_plugin_activate' );
Recommanded Articles
- Generate Dummy Posts in WordPress
- How to add captcha in Contact Form 7
- how to create a cron job in wordpress
- How to get form data in Contact Form 7
- Insert Update Delete Select query in WordPress
- How to increase website speed
- Create plugin in WordPress in which create table in DB on activate
- Create new tab under my account WooCommerce frontend
- Custom image sizes in WordPress
- Get posts by meta key and meta value in WordPress
Latest Comments