简体   繁体   English

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

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

I am new in WordPress plugin development.我是 WordPress 插件开发的新手。 This is my core PHP and HTML code这是我的核心 PHP 和 HTML 代码

create-table.html创建表.html

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

function.php函数.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.我想在我的 WordPress 插件开发中使用相同的过程。 Please any one help me.请任何人帮助我。

Thanks in advance.提前致谢。

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我已经为你评论过了: 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();
?>
    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"在“after_setup_theme”中添加此代码

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM