来自另一个文件的"register_activation_hook"

dan*_*y89 2 php wordpress

我是wordpress的新手,我想在我的插件中添加激活钩子.我想运行函数而不是从激活挂钩放置的同一个文件.可能吗?我试过了:

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');
Run Code Online (Sandbox Code Playgroud)

和install.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);
        }
Run Code Online (Sandbox Code Playgroud)

Rad*_*ire 8

我想运行函数而不是从激活挂钩放置的同一个文件.

你是在自问自答.

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

您的主插件文件可能包含:

include( 'initialize-plugin.php' );
register_activation_hook( __FILE__, 'install_dy_ressel' );
Run Code Online (Sandbox Code Playgroud)

initialize-plugin.php可以有这个功能:

function install_dy_ressel() {
    ...
Run Code Online (Sandbox Code Playgroud)