0

My plugin has several php files that are included on the main plugin file. I am using some functions on the other files but they don't work there. They work when I use them on the main plugin file.

For example,

On the main plugin file "test-plugin.php"

<?php
/*
Plugin Name: Test Plugin
Description: test
Version: 1.0
*/

include_once( plugin_dir_path( __FILE__ ) . 'test-file.php' );

On the other file "test-file.php" (same directory with test-plugin.php)

<?php

function enable_user_registration() {
    if(!get_option('users_can_register')) {
        update_option( 'users_can_register', '1' );
    }
}

register_activation_hook( plugin_dir_path(__FILE__), 'enable_user_registration' );

Regards..

0

I guess you should be using register_activation_hook() with the full path including filename, not just the path:

register_activation_hook( plugin_dir_path(__FILE__) . 'test-plugin.php', 'enable_user_registration' );
0

The first argument of register_activation_hook() is the plugin 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' );
    }
}
  • This is similar to the answer I already gave. Just you forgot, that this code is inside test-file.php and not inside the main plugin file. But the plugin activation will call the hook with the main plugin filename. See my answer. – JHoffmann Nov 8 '16 at 10:58
  • O yes, I didn't notice. Just a few minutes of difference, It seems I was written while you published your answer. As far I understand the source code of register_activation_hook(), the plugin activation will call the hook with any passed filename; not sure if passing a different filename can have negative consequences, but I guess not, I've seen how big and well known plugins use this method, as well in several development books. – cybmeta Nov 8 '16 at 11:22
  • When a plugin is activated, the action 'activate_PLUGINNAME' hook is called. In the name of this hook, PLUGINNAME is replaced with the name of the plugin, including the optional subdirectory. Quote from Codex So the hook is always called with the main plugin file name. register_activatio_hook won't throw an error if you use some other name, but the result is, that your function will not be called as it is hooked with the wrong tag. – JHoffmann Nov 9 '16 at 16:38
  • I understand what you mean, but actually my answer is correct, maybe the comment was not correct but the answer is. – cybmeta Nov 9 '16 at 17:10
  • 1
    Yes, of course. My comment was just in response to your comment. – JHoffmann Nov 9 '16 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.