Title: Adding a hook for custom plugin activation
Last modified: August 18, 2016

---

# Adding a hook for custom plugin activation

 *  [timvw](https://wordpress.org/support/users/timvw/)
 * (@timvw)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/)
 * (As posted at [http://timvw.madoka.be/?p=535](http://timvw.madoka.be/?p=535))
 * [blockquote]
    The WordPress documentation says that you have to call add_action(’
   activate_pluginurl’, ’somefunction’) to trigger somefunction when your plugin
   is activated. Unfortunately i couldn’t find with what pluginurl should be replaced.
   After a bit of experimenting i’ve found that in wp-admin/plugins.php the following
   is called when a plugin is activated:
 * do_action(‘activate_’ . trim( $_GET[‘plugin’] ));
 * So it appears that you simply have to use the path of your plugin relative to/
   wp-content/plugins. Eg: you have a plugin in /wp-content/plugins/wp-spamfilter/
   wp-spamfilter.php then you have to call add_action as following:
 * add_action(‘activate_wp-spamfilter/wp-spamfilter.php’, ’somefunction’);
    [/blockquote]
 * Is this conclusion right? Perhaps the documentation could better explain what
   exactly mypluginurl is…

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [paulfynch](https://wordpress.org/support/users/paulfynch/)
 * (@paulfynch)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359202)
 * Looking at another example, I think the url is just the file name, so your hook
   would look like this:
 * add_action(‘activate_myplugin.php’, ‘my_activate_function’);
 * The example I learned from is YARQ
    [http://openmonday.org/2006/07/31/yet-another-random-quote/](http://openmonday.org/2006/07/31/yet-another-random-quote/)
 * Cheers!
    pf
 *  [aaronfay](https://wordpress.org/support/users/aaronfay/)
 * (@aaronfay)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359203)
 * If your plugin is in a folder, use
 * add_action(‘activate_folder/plugin.php’, ‘function’);
 * Note, it has to be forward slash, backslash doesn’t seem to work (escapes the
   character)
 * Aaron
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 6 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359204)
 * A better way might be to find out what your current filename is, relative to 
   the plugins directory, and use that. Just coding off the top of my head here…
 * `$myrelpath = preg_replace('.*wp-content.plugins.','',__FILE__);
    add_action('
   activate_'.$myrelpath,'function');
 * This should work whether the plugin is in a subdirectory or not.
 *  [nkoenig](https://wordpress.org/support/users/nkoenig/)
 * (@nkoenig)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359205)
 * I’ve tried both solutions and I cannot make it work, I could really use some 
   hint here:
 * I’ve tried:
 * add_action(‘activate_folder/plugin.php’, ‘function’);
 * and also:
 * $myrelpath = preg_replace(‘.*wp-content.plugins.’,”,__FILE__);
    add_action(‘activate_’.
   $myrelpath,’function’);
 * But it seems that my function for installing my SQL TABLES
    doesn’t get initiated.
   Is there something that I’m missing? I have read CREATING TABLES WITH PLUGINS,
   but nothing seems to help.
 * I have put my install-function in its own php-file and placed it in ../plugins/
   along with my plogin.php file.
 *  [treehousetim](https://wordpress.org/support/users/treehousetim/)
 * (@treehousetim)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359208)
 * Hi nkoenig,
 * Try this code:
 * $activateFunction = ‘activateMyPlugin’; // change this;
 * add_action( ‘activate_’ . preg_replace( ‘/.*wp-content.plugins./’,”,__FILE__ ),
   $activateFunction );
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 5 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359209)
 * > I’ve tried:
   >  add_action(‘activate_folder/plugin.php’, ‘function’);
 * First, you’re not using that literally, are you? You do realize that you need
   to change the folder and filename to the folder and filename of your plugin, 
   right? Also you need to change the function to the name of your function, yes?
   The second version does the same thing, it just figures out the filename and 
   path for you, you still need to change the ‘function’ to whatever your install
   function is called.
 * > I have put my install-function in its own php-file and placed it in ../plugins/
   > along with my plogin.php file.
 * Okay, if it’s in its own PHP file, then you need to include or require it in 
   the actual plugin’s PHP file. Or just not have it in a separate file at all.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding a hook for custom plugin activation’ is closed to new replies.

## Tags

 * [adding](https://wordpress.org/support/topic-tag/adding/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [for?](https://wordpress.org/support/topic-tag/for/)
 * [hook](https://wordpress.org/support/topic-tag/hook/)

 * 6 replies
 * 6 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [19 years, 5 months ago](https://wordpress.org/support/topic/adding-a-hook-for-custom-plugin-activation/#post-359209)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
