The first argument of register_activation_hook()
is the filenameplugin filename including the path, so you need __FILE__
instead of plugin_dir_path( __FILE__ )
.
// This should be in main plugin file
register_activation_hook( __FILE__, 'enable_user_registration' );
function enable_user_registration() {
if(!get_option('users_can_register')) {
update_option( 'users_can_register', '1' );
}
}
It is common to register the activation hook in the main plugin file, but the callback can be defined in any file you wish.
For example, in test-file.php you can define the callback:
function enable_user_registration() {
if(!get_option('users_can_register')) {
update_option( 'users_can_register', '1' );
}
}
And in the main plugin file you can register the activation hook:
/*
Plugin Name: Test Plugin
Description: test
Version: 1.0
*/
include_once( plugin_dir_path( __FILE__ ) . 'test-file.php' );
register_activation_hook( __FILE__, 'enable_user_registration' );
You could also store the value of __FILE__
from the main plugin file in a constant, so you don't need to harcode any file name or file path:
/*
Plugin Name: Test Plugin
Description: test
Version: 1.0
*/
define( 'MY_PLUGIN_FILE_PATH', __FILE__ );
define( 'MY_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
include_once( MY_PLUGIN_DIR_PATH . 'test-file.php' );
And then, in test-file.php:
register_activation_hook( MY_PLUGIN_FILE_PATH, 'enable_user_registration' );
function enable_user_registration() {
if(!get_option('users_can_register')) {
update_option( 'users_can_register', '1' );
}
}