我已经在数据库中手动创建了一个表,它运行得很好.

现在,每当我激活我的模板时,我希望这个表在数据库中自动创建,所以我把下面的代码放在functions.php文件中:

register_activation_hook( get_template_directory()  . 'functions.php', 'Test_db_table' );

但激活模板后没有创建任何表.


Edit:

通过判断一个插件,我意识到它使用了以下代码:

register_activation_hook( plugin_dir_path( __FILE__ ) . 'simple.php', 'create_db_table' );

现在,我想为我的模板重写类似这样的内容,以便在激活模板时创建所需的表.


Edit2:

我也试过了,但还是没有得到答案:

register_activation_hook( __FILE__, 'Test_db_table' );

Edit3:

我希望确保问题不出在数据库中的表创建代码上;我创建了一个插件,并且只在其中放置了创建表的请求.

激活插件后,立即创建了一个表,该表使用我在functions.php文件中为它定义的规范.

Plugin creation codes:

<?php

/*
Plugin Name: Creates the database table 
Description: Creates the database table.
Author: Author
Author URI: https://example.com/
Text Domain: Creates_the_database_table 
Version: 1.0.0
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Creates the database table on plugin activation
register_activation_hook( plugin_dir_path( __FILE__ ) . 'test.php', 'Test_db_table' );

现在我正在寻找一种方法来对我的模板做同样的事情.

It means that the table will be created immediately after activating my template.

推荐答案

从问题中创建的最后一个示例开始,您可以为插件的activation hook和主题的after_switch_theme hook提供自定义函数.让我们调用函数'create_custom_table'.

在我们的函数中,我们需要包括WP管理文件watchde.php,因为它具有执行所需SQL所需的函数.

我们还可以使用$wpdb WordPress全局来轻松地获得所使用的表前缀,这样插件就可以更动态,并且前缀不是硬编码的.如果需要将字符集/排序规则信息添加到SQL中,还可以从$wpdb获取该信息.

我在代码中添加了注释,以便您可以跟随.这只是一个例子,所以请根据您的需要进行更改.

在插件中使用时:

register_activation_hook( __FILE__, 'create_custom_table' );

在主题中使用时:

add_action("after_switch_theme", "create_custom_table");

在这两种情况下添加的函数几乎是相同的,即示例函数:

function create_custom_table() {
    global $wpdb;

    // Get the prefix of the WordPress database
    $table_prefix = $wpdb->prefix;

    // Set the name of the table
    $table_name = $table_prefix . 'your_table_name';

    // Query for checking if table exists
    $table_exists = $wpdb->get_var("show tables like '$table_name'");

    // If the table does not exist, create it
    if (!$table_exists) {

    // Set create SQL   
    $sql = "CREATE TABLE `{$table_name}` (
     `field1` int(11) NOT NULL,
     `field2` varchar(200) DEFAULT NULL,
     KEY `your_index` (`field1`,`field2`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1";

    // Include upgrade.php so we get access to dbDelta function
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Eecute the SQL
    dbDelta($sql);

    }
}

Php相关问答推荐

调用woocommerce_customer_Save_add动作挂钩时发生致命错误

当我们使用PHPUnit时,控制台中的--uses param到底起了什么作用以及如何使用?

如何将对我的域的请求重定向到子文件夹中的索引,同时保留域URL并使用.htaccess?

Laravel eloquent-如果没有包含InitialValue的结果,则查询where Second Value

如何不重新查询模型A-&>;相关模型B-&>模型

向WooCommerce管理订单总额添加自定义总额行

将数据推入所有子数组条目

如何解决 Laravel 外键错误?

PHP 支持 APNG 文件吗?

按日期向 WooCommerce 中的我的帐户订单添加过滤器

根据小计向 WooCommerce Checkout 添加佣金

WooCommerce 相关产品(按 children 类别)作为排名数学主要类别的后备

无法读取某些 WordPress 主机上的 cookie

Shopware 6 AuthControllerDecorator已弃用

防止使用woocommerce_checkout_process下单

在PHP WordPress插件中使用变量连接路径时的问题解决方法

为什么foreach循环会输出每个结果?

Xdebug 不会在断点处停止

PHP 合并/合并多维数组

Laravel 8:会话不适用