Support » Plugins » Adding a hook for custom plugin activation

  • (As posted at

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

    Is this conclusion right? Perhaps the documentation could better explain what exactly mypluginurl is…

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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


    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)


    Moderator Samuel Wood (Otto)

    (@otto42) Admin

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

    This should work whether the plugin is in a subdirectory or not.

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

    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.



    Hi nkoenig,

    Try this code:

    $activateFunction = ‘activateMyPlugin’; // change this;

    add_action( ‘activate_’ . preg_replace( ‘/.*wp-content.plugins./’,”,__FILE__ ), $activateFunction );

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    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.