For more information on what will be passed to your $callback
, please see the activate_{$plugin} hook.
Related discussion with another sample of working code: https://wordpress.org/support/topic/312342
Registering the hook inside the ‘plugins_loaded’ hook will not work. You can’t call register_activation_hook() inside a function hooked to the 'plugins_loaded' or 'init' hooks (or any other hook). These hooks are called before the plugin is loaded or activated.
When a plugin is activated, all active plugins are loaded, then the plugin is activated. The plugin’s activation hook is run and then the page is immediately redirected
If you are interested in doing something just after a plugin has been activated it is important to note that the hook process performs an instant redirect after it fires. So it is impossible to use add_action() or add_filter() type calls until the redirect has occurred (e.g., only two hooks are fired after the plugin’s activation hook: 'activated_plugin' and 'shutdown'). A quick workaround to this quirk is to use add_option() like so:
/* Main Plugin File */
...
function my_plugin_activate() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'my_plugin_activate' );
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
}
}
add_action( 'admin_init', 'load_plugin' );
You can check out the full post @ http://stackoverflow.com/questions/7738953/is-there-a-way-to-determine-if-a-wordpress-plugin-is-just-installed/13927297#13927297.
However, it is possible to use do_action(), like this:
function my_plugin_activate() {
do_action( 'my_plugin_activate' );
}
register_activation_hook( __FILE__, 'my_plugin_activate' );
Included plugin files and even other plugins will be able to hook into this action.
If you’re using global variables, you may find that the function you pass to register_activation_hook() does not have access to global variables at the point when it is called, even though you state their global scope within the function like this:
$myvar = 'whatever';
function myplugin_activate() {
global $myvar;
echo $myvar; // this will NOT be 'whatever'!
}
register_activation_hook( __FILE__, 'myplugin_activate' );
This is because on that very first include, your plugin is NOT included within the global scope. It’s included in the activate_plugin() function, and so its “main body” is not automatically in the global scope.
This is why you should always be explicit. If you want a variable to be global, then you need to declare it as such, and that means anywhere and everywhere you use it. If you use it in the main body of the plugin, then you need to declare it global there too.
When activation occurs, your plugin is included from another function and then your myplugin_activate() is called from within that function (specifically, within the activate_plugin() function) at the point where your plugin is activated. The main body variables are therefore in the scope of the activate_plugin() function and are not global, unless you explicitly declare their global scope:
global $myvar;
$myvar = 'whatever';
function myplugin_activate() {
global $myvar;
echo $myvar; // this will be 'whatever'
}
register_activation_hook( __FILE__, 'myplugin_activate' );
More information on this is available here: https://wordpress.org/support/topic/201309
Note that register_activation_hook must not be registered from within another hook for example ‘plugins_loaded’ or ‘init’ as these will have all been called before the plugin is loaded or activated.
This will NOT work:
Singleton class pattern
If your plugin uses the singleton class pattern, add the activation hook like so:
If the class that holds your activation function/method is in some additional file, register your activation function like this:
Or, because the activation hook requires a static function, if you’re inside of a __construct():
There’s a comment here that could make this confusing by giving incorrect information. The code does NOT have to be in the main file, unless you’re doing something like a single file plugin. The thing to know is that the first argument is the name required to get your code to fire, not what file the code is in. Example:
Main plugin file: plugin/myplugin/myplugin.php
include 'some_class.php';
$obj = new other_class();
Other plugin file: plugin/myplugin/some_class.php
class some_class {
__constructor() {
register_activation_hook(__DIR__.'/myplugin.php',array($this,'activate');
}
public function activate() { ... }
}
If calling this within a namespace:
Source: http://stackoverflow.com/questions/37863766/wordpress-not-recognizing-function-during-plugin-activation
According to @nacin (a lead developer for WordPress), you shouldn’t use activation hooks (especially on multisite). You should do this instead:
“It’s far better to use an upgrade routine fired on admin_init, and handle that per-site, basing it on a stored option.”
Source: https://core.trac.wordpress.org/ticket/14170#comment:68
The hooked function has one boolean argument,
$network_wide
which indicates if the plugin is network activated.admin_init
fires on every admin page load, but if you follow Nacin’s recommendation and condition the callback on a stored option, then the cost is near-zero.Sending arguments with callable function
Imagine that you have OOP structure for your plugin and need to observe SOLID principle inside it.
For this reason, you need to Dependency Injection with callable function inside your register_activation_hook function.
Sample code is like in the following:
Note: I used both way to show calling callable function with argument and without it in activation and deactivation hooks to bold using each of them. Usage of callable function with argument is in register_activation_hook.
You can flush permalink if you have custom rewrite rules in your plugin
Really useful when, for example, when we need to create a DB table for our plugin functionalities:
function create_ourtable(){
global $wpdb;
$prefix = $wpdb->prefix;
$form_db = $prefix . "ourtable";
//Check if table exists. In case it's false we create it
if($wpdb->get_var("SHOW TABLES LIKE '$form_db'") !== $form_db){
$sql = "CREATE TABLE $form_db(id mediumint unsigned not null primary key auto_increment, dates timestamp, names varchar(500), wp_user mediumint unsigned)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
If you’re looking for a similar hook that fires when a theme is activated, see after_switch_theme.
Note that the WordPress code sniffer rules will reject the hook name that is constructed by this function, if you ever have to call the hook manually (for example, from a test). The
WordPress.NamingConventions.ValidHookName.UseUnderscores
rule rejects slashes and dots in hook names.Please note that register_activation_hook() must be called from the main plugin file – the one that has “Plugin Name: …” directive.
This code added in the main plugin file will work:
However, if you try to add register_activation_hook() in the constructor of the class it won’t work because it’s not in the main plugin file.
If you have a function called
myplugin_activate()
in the main plugin file at eitherwp-content/plugins/myplugin.php or
wp-content/plugins/myplugin/myplugin.php
use this code:
This will call the
myplugin_activate()
function on activation of the plugin.