Create tables with wordpress plugin

If you are making the wordpress plugin and in wordpress plugin many times we need to create new tables. In this article I will show you how easily we can create the wordpress plugin.
Using following code you can create the table in wordpress database.

global $jal_db_version;
$jal_db_version = "1.0";

function jal_install () {
   global $wpdb;
   global $jal_db_version;

   $table_name = $wpdb->prefix . "liveshoutbox";
   if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

      $sql = "CREATE TABLE " . $table_name . " (
	  id mediumint(9) NOT NULL AUTO_INCREMENT,
	  time bigint(11) DEFAULT '0' NOT NULL,
	  name tinytext NOT NULL,
	  text text NOT NULL,
	  url VARCHAR(55) NOT NULL,
	  UNIQUE KEY id (id)

      require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

      $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' => $welcome_text ) );

      add_option("jal_db_version", $jal_db_version);


Now that we have the initialization function defined, we want to make sure that WordPress calls this function when the plugin is activated by a WordPress administrator. To do that, we will use the activate_ action hook. If your plugin file is wp-content/plugins/plugindir/pluginfile.php, you’ll add the following line to the main body of your plugin:


You may like following Articles!

  • jim

    this is awesome man

  • LG GD510

    Have you ever considered about adding a little bit more than just your articles? I mean, what you say is valuable and all. But imagine if you added some great images or videos to give your posts more, “pop”! Your content is excellent but with pics and clips, this blog could definitely be one of the best in its niche. Excellent blog!

  • Isis Biehl

    Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. I’ll tell you about that later…