gpt4 book ai didi

php - "register_activation_hook"来自另一个文件

转载 作者:行者123 更新时间:2023-12-04 22:32:15 24 4
gpt4 key购买 nike

我是 wordpress 的新手,我想在我的插件中添加激活钩子(Hook)。我不想从放置激活钩子(Hook)的同一个文件中运行函数。是否可以?我试过这样:

dy_ressel.php(主插件文件):

$my_variable_for_identify_dir = plugin_dir_url( __FILE__ ) ;
register_activation_hook( $my_variable_for_identify_dir.'install.php','install_dy_ressel');

并安装.php
function install_dy_ressel(){
global $wpdb;

// Добавить к названию будущих табли префикс WP
$table_users = $wpdb->prefix . "dy_users";


// ПРоверка по наличию таблиц. Если нету или удалены - создать.

if($wpdb->get_var("SHOW TABLES LIKE '$table_users'") != $table_users) {
$sql = "CREATE TABLE " . $table_users . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time bigint(11) DEFAULT '0' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) NOT NULL,
UNIQUE KEY id (id)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}

最佳答案

I want to run function not from the same file where activation hook placed.



你是在自问自答。

您只需要将激活 Hook 放在您的主插件文件中。但它可以在任何地方的功能,只要文件在被 WordPress 调用之前被包含。

您的主插件文件可能有:
include( 'initialize-plugin.php' );
register_activation_hook( __FILE__, 'install_dy_ressel' );

还有你的 initialize-plugin.php可以具有以下功能:
function install_dy_ressel() {
...

关于php - "register_activation_hook"来自另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com