Advertise on Site! You can Advertise on our site. Find out More

Create tables with wordpress plugin

8 Flares Twitter 7 Facebook 1 Google+ 0 StumbleUpon 0 LinkedIn 0 Buffer 0 Filament.io 8 Flares ×


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');
      dbDelta($sql);

      $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:

register_activation_hook(__FILE__,'jal_install');
8 Flares Twitter 7 Facebook 1 Google+ 0 StumbleUpon 0 LinkedIn 0 Buffer 0 Filament.io 8 Flares ×
  • http://www.novusbalivillas.com/free-antivirus-downloads-today/ jim

    this is awesome man

  • http://lggd510.com/ 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!

  • http://uautoclub.com/ 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…

8 Flares Twitter 7 Facebook 1 Google+ 0 StumbleUpon 0 LinkedIn 0 Buffer 0 Filament.io 8 Flares ×