1

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.

1
  • Where are you hoping to use this? Why are you needing to do this?
    – s_ha_dum
    Mar 24, 2014 at 13:44

2 Answers 2

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");
}
3
  • Thanks @Rajeev Vyas. How to display forms in back end of wordpress.. i mean wordpress admin page
    – Boopathi
    Mar 24, 2014 at 5:57
  • @Boopathi have you created page in wordpress admin? Mar 24, 2014 at 5:58
  • yes. but i dont know how to create more pages in wordpress. i am having single index.php file only
    – Boopathi
    Mar 24, 2014 at 6:01
-1
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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.