0

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 here

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.

4
  • whats the problem? specific please! Commented Mar 24, 2014 at 5:50
  • I dont know how to create form in wordpress and after form submission create table in wordpress database. @Adrian Preuss Commented Mar 24, 2014 at 6:16
  • WHERE do you want to create a table? Frontend, backend ...? Commented Mar 24, 2014 at 6:23
  • create table in backend Commented Mar 24, 2014 at 6:28

3 Answers 3

2

You have very many options for that.

Here is one of them as a little plugin. I've commented it for you: https://hostr.co/pRBSmTkZ2LlJ

<?php
    /*
        Plugin Name:    stackoverlow - My own Table
        Version:        1.0.0
    */

    class My_Table {

        /*
            Add an menu entry in the Administration. Fore more information see: http://codex.wordpress.org/Administration_Menus
        */
        public function __construct() {
            add_action('admin_menu',    array($this, 'admin_menu'));
        }

        public function admin_menu() {
            $menu_title = 'Table: Title of your Menu';  // The title of your menu entry
            $menu_slug  = 'my_table';                   // The slug, for example: wp-admin/admin.php?page=my_table
            add_menu_page($menu_title, $menu_title, 'manage_options', $menu_slug, array($this, 'admin_page'));
        }

        /*
            Here is the Output of your Page
        */
        public function admin_page() {
            global $wpdb;

            // Handle here your Data
            if(isset($_POST['create'])) {
                $table_name = $_POST['table_name'];

                // WARNING: SQL Injections - Data not prepared!
                $wpdb->query("CREATE TABLE IF NOT EXISTS `" . $table_name . "`;");
            }

            if(!empty($wpdb->last_error)) {
                printf('<pre>ERROR: %s</pre>', $wpdb->last_error);
            }

            if(!empty($wpdb->last_query)) {
                printf('<pre>Query: %s</pre>', $wpdb->last_query);
            }
            ?>
            <form method="post" action="<?php print admin_url('admin.php?page=my_table'); ?>">
                <input type="text" name="table_name" />
                <input type="submit" name="create" />
            </form>
            <?php
        }
    }

    new My_Table();
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I cant get that file. it shows something like. This file belongs to an account that hasn't been activated yet.
Thank you so much @ Adrian Preuss.
0
    global $wpdb;
global $table_db_version;
$table_add_one = $wpdb->prefix."store_rating";
$store_create_ddl="
CREATE TABLE IF NOT EXISTS `".$table_add_one."` (
  `rid` INT(11) UNSIGNED AUTO_INCREMENT,
  `ip` varchar(255) NOT NULL,
  `device` varchar(255) NOT NULL,
  `user_agent` varchar(255) NOT NULL,
  `storeid` int(11) NOT NULL,
  `rating` int(1) NOT NULL,
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(rid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
    

$all_tables = array();
$mytables=$wpdb->get_results("SHOW TABLES");
foreach ($mytables as $mytable){
    foreach ($mytable as $t){                   
        $all_tables[]=$t;
    }
}



$sql_one='';
if(!in_array($table_add_one,$all_tables)){
    $sql_one.=$store_create_ddl;
    if(!empty($sql_one)){
        if(!function_exists('wp_should_upgrade_global_tables')){
            require(ABSPATH . 'wp-admin/includes/upgrade.php');
        }
        dbDelta($store_create_ddl);
    }       
}  

add this code in "after_setup_theme"

Comments

-1

Here is a sample code to create a table using a plugin

// Make custom table
function add_custom_table_on_install()
{
    global $wpdb;   

    $table_name = $wpdb->prefix . "my_table";

    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
            id BIGINT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
            name VARCHAR( 255 ) NOT NULL
        )";
        $wpdb->query($wpdb->prepare($sql));
    }
}
// Custom table hook
register_activation_hook(__FILE__, 'add_custom_table_on_install');

3 Comments

I want to create table after Installation.
Did you give a try to the code I have posted? This code will create a table as soon as you install and activate your plugin. This is what i was able to understand with the minimum details you have posted about your question
okay @Zameer Khan. But I want to create more than one table after installing the plugin. Should be able to create new tables from my plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.