如何使用我自己的插件在WordPress中创建自定义表?

时间:2014-03-24 05:39:49

标签: php mysql database wordpress

我是WordPress插件开发的新手。这是我的核心PHP和HTML代码

创建-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");
}

我想在我的WordPress插件开发中使用相同的过程。请任何人帮助我。

提前致谢。

3 个答案:

答案 0 :(得分:1)

你有很多选择。

这是其中一个作为一个小插件。我已经为你评论过了: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();
?>

答案 1 :(得分: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);
    }       
}  

将此代码添加到“ after_setup_theme”中

答案 2 :(得分:-1)

以下是使用插件

创建表格的示例代码
// 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');