Create plugin in WordPress in which create table in DB on activate

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

 

Also Read: How to increase website speed
Also Read: Create new tab under my account WooCommerce frontend

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

 

Also Read: How to increase website speed
Also Read: Create new tab under my account WooCommerce frontend

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close