Wordpress is a great platform for web development, and we all know about that we can make highly desgine full, functionalable site with wordpress. WordPress is developed tremendously in recent four, five years. It’s themes and plugins are just awesom for creating website. But some times we need to create our own custom plugin for many reson. Tody my topic is to create custom table when you will activate your plugins. So many people need that they will create their own custom table in wordpress db and they will access it from front-end/back-end. So just follow my step to do this.
Step1:
First of all install wordpress, if you haven’t done it then do it first by visit this link WordPress Install. Now go to the project folder and enter to this path “wp-content\plugins“. Now create a folder named it with your plugin names. Then enter to this folder and create a php file, named it with your plugin names and “.php” extension. Now we have to ready our plugins by adding the bellow code in to this php file.
<?php /** * Plugin Name: Your first plugin * Plugin URI: http://abcd.com * Description: This plugin is used to create custom table in database * Version: 1.0.0 * Author: Your Name * Author URI: http://abcd.com * License: GPL2 */ ?>
Now our plugin is ready for activation. So just go to the admin panel and activate it. Here you can give your all information in this plugins.
Step 2:
Now we have to use the code for generating custom table. Just add the below code in your plugin file.
function create_database_table_wp_mycustomtable(){ global $wpdb; $db_table_name = $wpdb->prefix . 'mycustomtable'; // table name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $db_table_name ( id int(10) NOT NULL auto_increment, name text NOT NULL, UNIQUE KEY id (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } register_activation_hook( __FILE__, 'create_database_table_wp_mycustomtable');
If everything is ok then you can check your database and can see a table with “wp_mycustomtable” name. Please put a comment if any thing happen wrong. I am here to help you.