Create Table in WordPress Database on Plugin Activation

Reading Time: 5 minutes
5,138 Views

Inside this article we will see how to create table in wordpress database on plugin activation. Article contains very classified information about generating dynamic table on plugin activation.

After this article you will be sure how to create your own database tables for wordpress setup for your own custom plugin. We usually create own tables in wordpress database when we need more data to be stored for custom plugin functions.

Apart from creating tables also we can add more functions which we want should be executed at the time of activation.

WordPress works on the basis of filters and hooks. WordPress also provides a activation hook. We will see here into complete detail.

Learn More –

Let’s get started.


Plugin Activation Hook

WordPress provide a hook for activation. We use this activation to attach all functions that we need on plugin activation or theme activation.

register_activation_hook(__FILE__, callback);

callback function contains all code which will be executed when plugin/theme activates. We will add our dynamic table generation code into this.


Code To Generate Table

Here, is the steps to generate dynamic table.

  • Add Activation hook
  • Callback function
register_activation_hook(__FILE__, 'mytable_activation_function');

// callback function to create table
function mytable_activation_function()
{
    global $wpdb;

    $mytable = 'QUERY TO GENERATE TABLE';

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($mytable);
}

Plugin Setup

We will see dynamic table generation using a plugin. So, we will create a custom plugin here.

Create a folder mytable inside /wp-content/plugins folder. Inside this mytable folder, create plugin entry file mytable.php.

Open mytable.php and write this code into it.

<?php

/**
 * Plugin Name:       OWT MyTable
 * Plugin URI:        https://sample-plugin.com
 * Description:       This plugin when activates, create a table inside database.
 * Version:           1.0
 * Author:            Online Web Tutor
 * Author URI:        https://onlinewebtutorblog.com/
 */

// plugin activation hook
register_activation_hook(__FILE__, 'mytable_activation_function');

// callback function to create table
function mytable_activation_function()
{
    global $wpdb;

    if ($wpdb->get_var("show tables like '" . owt_create_my_table() . "'") != owt_create_my_table()) {

        $mytable = 'CREATE TABLE `' . owt_create_my_table() . '` (
                            `id` int(11) NOT NULL AUTO_INCREMENT,
                            `name` varchar(100) NOT NULL,
                            `email` varchar(50) NOT NULL,
                            `status` int(11) NOT NULL DEFAULT "1",
                            `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                            PRIMARY KEY (`id`)
                          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;';

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($mytable);
    }
}

// returns table name
function owt_create_my_table()
{
    global $wpdb;
    return $wpdb->prefix . "mytable";
}

Once plugin will setup inside wordpress, you should see inside Plugins >> Installed Plugins

Click on Activate

Successfully we have created a table for custom plugin.

We hope this article helped you to learn Create Table in WordPress Database on Plugin Activation Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.