我已经在数据库中手动创建了一个表,它运行得很好.
现在,每当我激活我的模板时,我希望这个表在数据库中自动创建,所以我把下面的代码放在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.