I’m new in wordpress and I want to add activation hook in my plugin. I want to run function not from the same file where activation hook placed. Is it possible? I tried so:
dy_ressel.php (main plugin file) :
$my_variable_for_identify_dir = plugin_dir_url( __FILE__ ) ;
register_activation_hook( $my_variable_for_identify_dir.'install.php','install_dy_ressel');
And 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);
}
You answered your own question.
You just need to place the activation hook in your main plugin file. But the function that it can be anywhere, so long as the file is included before it is called by WordPress.
Your main plugin file might have:
And your
initialize-plugin.php
can have the function:register_activation_hook
doesn’t need to be called from the plugin’s main file, as long as you have some way of communicating the plugin’s main file’s path to that other piece of code. Here’s a simple way to do that:index.php
(or whatever you call your plugin’s main file):install.php
:dy_ressel.php
install.php:
if your function is in a class
$class = new Name_of_class(); //initialize it first