WordPress.org

Ready to get started?Download WordPress

Forums

Adding a hook for custom plugin activation (7 posts)

  1. timvw
    Member
    Posted 8 years ago #

    (As posted at 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...

  2. paulfynch
    Member
    Posted 7 years ago #

    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/

    Cheers!
    pf

  3. aaronfay
    Member
    Posted 7 years ago #

    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

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  5. nkoenig
    Member
    Posted 7 years ago #

    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.

  6. treehousetim
    Member
    Posted 7 years ago #

    Hi nkoenig,

    Try this code:

    $activateFunction = 'activateMyPlugin'; // change this;

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

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic