add new tables using wordpress theme

How to create or add new tables using wordpress theme

With custom professional theme you need to add the custom table in wordpress database, Using this code you can, add new tables using wordpress theme

add new tables using wordpress theme

If you want to add one more table to wordpress database you need to just use the following code.

Open the functions.php file and copy paste the code.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]


$wordpressapi_db_version = "1.0";

global $wpdb;
global $wordpressapi_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);

$welcome_name = "Mr. WordPress";
$welcome_text = "Congratulations, you just completed the installation!";

$insert = "INSERT INTO " . $table_name .
" (time, name, text) " .
"VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";

$results = $wpdb->query( $insert );

add_option("wordpressapi_db_version", $wordpressapi_db_version);

}

[/viral-lock]

register_activation_hook(__FILE__,'YOUR_FUNCTION_NAME');
add new tables using wordpress theme
add new tables using wordpress theme

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “How to create or add new tables using wordpress theme”

Leave a Reply

Your email address will not be published.