Skip to main content
2 of 6
added 505 characters in body
cybmeta
  • 20.6k
  • 5
  • 47
  • 56

The first argument of register_activation_hook() is the filename including the path, so you need __FILE__ instead of plugin_dir_path( __FILE__ ).

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

As opposed to the other answer, main plugin filename is NOT required; in fact, it is ver common to pass the filename where the activation hook is registered. You can see examples in the codex like this:

include_once dirname( __FILE__ ) . '/your_additional_file.php';
register_activation_hook( __FILE__, array( 'YourAdditionalClass', 'on_activate_function' ) );

And the source code of register_activation_hook() creates actions based on the filename passed, it is not require main plugin filename.

cybmeta
  • 20.6k
  • 5
  • 47
  • 56