
register_activation_hook is very important function for creating the wordpress plugin. The function register_activation_hook registers a plugin function to be run when the plugin is activated.
You can use the register_activation_hook as follows:
<?php register_activation_hook($file, $function); ?>
If you can going to create the wordpress simple plugin then this function is very useful. this function you can use as follows in files.
If you have a function called myplugin_activate() in the main plugin file at either
* wp-content/plugins/myplugin.php or
* wp-content/plugins/myplugin/myplugin.php
you can use code as follows:
global $myvar;
$myvar='whatever';
function myplugin_activate() {
global $myvar;
echo $myvar; // this will be 'whatever'
}
register_activation_hook( __FILE__, 'myplugin_activate' );
1. register_activation_hook() must be called from the main plugin file – the one that has “Plugin Name: …” directive.
2. Your hook function must be in the same file as well. From that function it is ok to call other functions defined in other files.
3. Doing echo “My hook called!”; – does not work from it. So this is not a good way to judge whether it working or not.
4. Your global variables must be explicitly declared as global for them to be accessed from inside of my_activation_hook().
Check the sample wordpress plugin code.
<?php
/*
Plugin Name: A Test
Description: A Test
*/
require_once (dirname(__FILE__) . '/my_other_file.php');
// This code *will not* work. Activation hook function must be defined in the main plugin file.
// register_activation_hook (dirname(__FILE__) . '/my_other_file.php', 'my_other_function');
// This code will work.
register_activation_hook (__FILE__, 'test_activated');
// This is correct way to declare/access globals.
global $some_var; // globals must be declared explicitly. Without this you will not be able to access '$some_var' from within 'test_activated()' function.
$some_var = 'hey';
//===========================================================================
function test_activated ()
{
global $some_var; // Now it will be 'hey'.
// This function is defined in 'my_other_file.php'
my_other_function ();
// This will not work, so don't try. If you need logging write something in temporary log file in here via fopen/fwrite.
// If you want to quickly test if your activation hook works - put exit() into it. At least you'll see error during activation.
echo 'test_activated called!';
}
//===========================================================================
?>
The function register_deactivation_hook registers a plugin function to be run when the plugin is deactivated.
following function will deactivate your plugin
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
Incoming search terms:
- wordpress register hook
- activation hooks wordpress plugin
- google code wordpress register activation hook
- plugin activated hook wordpress
- register activation hook
- wordpress activation hook not getting called
- wordpress plugin activation hook not working






The excellent article helped me very much! Saved the website, very interesting categories everywhere that I read here! I like the info, thank you.
Just discovered your website on yahoo and that i think it is a shame that you aren’t ranked higher because this really is a fantastic put up. To change this I decided to save your internet site to my RSS reader and that i will try to mention you in a single of my posts since you genuinely deserv a lot more readers when publishing content of this quality.
Please tell me it worked right? I dont want to sumit it again if i do not have to! Either the blog glitced out or i am an idiot, the second option doesnt surprise me lol. thanks for a great blog!