register activation hook using in wordpress plugin

wordpress hooks are very important for wp developers. We can use the wp hooks in wordpress plugin and themes. Here in this article I will show you how to register the hook in wordpress plugin. I given the simple code sample which will give you the fare idea about using the wordpress hooks.

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;

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.

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' );

You may like following Articles!

  • Chauncey

    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.

  • Adair

    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!

  • Tawnya

    Woah! I’m really loving the template/theme of this site. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between superb usability and visual appearance. I must say that you’ve done a great job with this. In addition, the blog loads very fast for me on Safari. Superb Blog!

  • Irvin

    This is the perfect web site for everyone who would like to find out about this topic. You know so much its almost hard to argue with you (not that I actually would want to…HaHa). You certainly put a brand new spin on a topic which has been discussed for a long time. Wonderful stuff, just wonderful!

  • Eldon

    I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. appreciate it