$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to create custom tables in WordPress using my own plugin?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to create custom tables in WordPress using my own plugin?

matteradmin9PV0评论

I am new in WordPress plugin development. This is my core PHP and HTML code

create-table.html

  <form method="post" action="function.php">
    <input type="text" name="table_name">
    <input type="submit" name="create">
    </form>

function.php

if(isset($_POST['create'])
{
$table-name=$_POST['table_name'];

//create table query
header("location: add_table_attribute.php");
}

I want to use this same process in my WordPress plugin development. Please any one help me.

Thanks in advance.

I am new in WordPress plugin development. This is my core PHP and HTML code

create-table.html

  <form method="post" action="function.php">
    <input type="text" name="table_name">
    <input type="submit" name="create">
    </form>

function.php

if(isset($_POST['create'])
{
$table-name=$_POST['table_name'];

//create table query
header("location: add_table_attribute.php");
}

I want to use this same process in my WordPress plugin development. Please any one help me.

Thanks in advance.

Share Improve this question edited Mar 24, 2014 at 5:47 Boopathi asked Mar 24, 2014 at 5:33 BoopathiBoopathi 1151 silver badge9 bronze badges 1
  • Where are you hoping to use this? Why are you needing to do this? – s_ha_dum Commented Mar 24, 2014 at 13:44
Add a comment  | 

2 Answers 2

Reset to default 2

Creating Tables with plugin This page shows how to work with tables in plugins. Example on that page includes table creation during installation of plugin. But it can be dynamically used also to create table. See below.

if(isset($_POST['create'])
{
$table_name=$_POST['table_name'];

Note : don't use - in variable names.

global $wpdb;
$sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  name tinytext NOT NULL,
  text text NOT NULL,
  url VARCHAR(55) DEFAULT '' NOT NULL,
  UNIQUE KEY id (id)
);";

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

//create table query
header("location: add_table_attribute.php");
}
function create_custom_table()
{
    global $table_prefix, $wpdb;
    $tblname = 'custom_plugin';
    $wp_track_table = $table_prefix . "$tblname ";
    #Check to see if the table exists already, if not, then create it
    if($wpdb->get_var( "show tables like '$wp_track_table'" ) != $wp_track_table)
    {
        $sql = "CREATE TABLE `wp_custom_plugin` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `name` varchar(100) NOT NULL,
                `email` varchar(100) NOT NULL,
                `phone` varchar(100) NOT NULL,
                `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                 PRIMARY KEY (`id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ";
        require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
        dbDelta($sql);
    }
}

 register_activation_hook( __FILE__, 'create_custom_table' );
Post a comment

comment list (0)

  1. No comments so far