php - 如何修复显示的 WordPress 自定义插件问题 - "Database Update Required"

标签 php wordpress

如何修复显示“需要更新数据库”的 WordPress 自定义插件问题 - 即使插件工作正常。我附上了错误/警告消息的图像。代码片段包括插件的所有源代码。我不知道如何删除错误/警告消息。

enter image description here

<?php
/**
* Plugin Name: Content Creator 2 plugin
* Plugin URI: http://www.nodomain.com
* Description: This plugin will create a simple HTML-form with 3 input fields and store the information into a stage table.
* Version: 1.0
* Author: Murat Kilic
* Author URI: http://www.nodomain.com
* License: GPLv3
**/

function insert_into_db() {
    # This ($wpdb) is a global variable inside WordPress that gives me access to create new tables inside WordPress
    global $wpdb;
    
    #Create a new table inside my existing wp-database if table not exists
    $table = $wpdb->prefix . "cc2testplugin";
    #Setting the charset for the database
    $charset_collate = $wpdb->get_charset_collate();
    # Creating table with SQL
    $sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` MEDIUMINT NOT NULL AUTO_INCREMENT,
    `stagename` TEXT NOT NULL,
    `capacity` MEDIUMINT NOT NULL,
    `url` TEXT NOT NULL,
    UNIQUE (`id`)
    ) $charset_collate;";
    
    #Including upgrade.php file
    require_once( ABSPATH . 'wp-admin/upgrade.php');
    
    #Useful for creating new tables and updating existing tables to a new structure.
    dbDelta ($sql);
    
    #Starts output buffering
    ob_start();
    
    ?>
      
    <!-- Here I will create my HTML_form with 3 input-fields  -->
    <form action="#v_form" method="post" id="v_form">
        <label for="stagename">
            <h3>Name of stage:</h3>
        </label>
        <input type="text" name="stagename" id="stagename">
        
        <label for="capacity">
            <h3>Max capacity of stage:</h3>
        </label>
        <input type="number" min="1000" max="50000" name="capacity" id="capacity">
        
        <label for="url">
            <h3>Type MAP location (url):</h3>
        </label>
        <input type="url" name="url" id="url">
        <input type="submit" value="submit" name="submit_form">
    </form>
    <!-- END HTML-form -->
      
    <?php
        #Gets the current buffer contents and delete current output buffer
        $html = ob_get_clean();
        # Check if the user has clicked on the submit button
        if (isset ($_POST["submit_form"])  ) {
            #Setting table prefix
            $table = $wpdb->prefix . "cc2testplugin";
            #Collecting data from input fields and store it in variables
            $stagename = strip_tags($_POST["stagename"], "");
            $capacity = strip_tags($_POST["capacity"], "");
            $url = $_POST["url"];
            
            #Inserting data from the HTML-form based on the variables into the table
            $wpdb->insert(
                $table,
                array(
                    'stagename' => $stagename,
                    'capacity' => $capacity,
                    'url' => $url
                )
            );
            #Writing out a message after having inserted the data from the HTML-Form
            $html = "<p>The stage with following name <strong>$stagename</strong> was successfully inserted! Thanks.</p>";
        }
    # Displays /outputs text on screen
    return $html;
}
    #Adding a wordpress shortcode that can be used on either a wordpress page or post
    add_shortcode('cc2testplugin','insert_into_db');    
    ?>

最佳答案

我在这里看到的问题是,您在每次运行带有短代码的页面时都在检查是否创建表。当您创建表格时,您包含了 upgrade.php,它将显示您描述的消息。

您需要做的是断开创建表的部分(包括对 upgrade.php 的调用)并将其添加到您的激活插件部分。您应该只需要检查并创建该表一次。不要忘记在卸载插件时删除表格(以及您添加的任何其他内容)。

在这里阅读更多相关信息: https://codex.wordpress.org/Creating_Tables_with_Plugins

关于php - 如何修复显示的 WordPress 自定义插件问题 - "Database Update Required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50530103/

相关文章:

php - PHP 中 preg_replace 的正则表达式

PHP获取url内容然后将表中的元素解析为JSON

php - 自定义电子邮件未在 WooCommerce 中完成订单发送

css - 制作没有图像的 Woocommerce 产品子类别小盒子

php - 使用 codeigniter 显示数据库中的图像及其附带信息

javascript - Jquery 单选按钮提交多次重复相同的值

php - 使用 Simple HTML Dom 获取所有 HTML 列表元素

php - WordPress - 在 'wp get attachment image' 中包含页面标题

php - wp_dropdown_pages 作为我的小部件中的一个选项

php - 文本区域中的 WordPress SQL 命令