#4389 closed enhancement (invalid)
Improve plugin activate and deactivate hooks
| Reported by: | Otto42 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Optimization | Version: | |
| Severity: | major | Keywords: | |
| Cc: | Focuses: |
Description
The activate and deactivate hooks are clumsy, at best. Because they rely on the name of the plugin file being a constant, renaming a plugin file will break them. Even worse, the plugin path is part of that name, so putting a plugin in a subdirectory that it was not expecting (for users that want to keep things organized) will also break these.
Instead, how about adding an optional tag to the Plugin header, like so:
<?php /* Plugin Name: Name Of The Plugin Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates Description: A brief description of the plugin. Version: The plugin's Version Number, e.g.: 1.0 Author: Name Of The Plugin Author Author URI: http://URI_Of_The_Plugin_Author Activate: activate_function_hook Deactivate: deactivate_function_hook */ ?>
When a plugin is activated or deactivated, that tag can be read from its header and then the hook specified can be called to trigger that action in the plugin. This provides an absolutely certain way to call activate or deactivate functions in a plugin, without relying on the filename and filepath that the plugin happens to be in.
Change History (6)
#2
@
19 years ago
That still doesn't address the issue of renaming a plugin before deactivating it, or does it?
#4
@
19 years ago
{{{FILE}} is dynamic and the hook is dynamic. So whatever the filename is when you deactivate, that's the filename that'll be used for the hook.
If you rename (or delete) an active plugin, it will cease to be used, and will not fire the deactivation hook (because we don't know where the plugin is, and thus can't find its deactivation hook). That's a separate issue (and not one I think we should fix... being able to delete/rename a plugin is necessary if an FTP edit causes your WP admin to deliver a fatal error.
#5
in reply to: ↑ 1
@
19 years ago
- Resolution invalid
- Status closed → reopened
Replying to markjaquith:
We have API for this. You're not supposed to use the hooks directly.
register_activation_hook(__FILE__, 'your_activation_function');By using
__FILE__, you ensure that it will work no matter what name is given to the plugin.
Please re-open if this doesn't address your concern.
That call won't actually work though, will it? FILE is the complete and absolute path of the php file, like /home/users/whatever/web/wp-content/plugins/myplugin.php or some such thing, whereas the actual hook looks like this:
@include(ABSPATH . PLUGINDIR . '/' . $plugin);
...
do_action('activate_' . $plugin);
Sure looks like it's calling activate_myplugin.php instead, to me.
So perhaps this ticket needs to be "Fix the broken API" instead?
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
We have API for this. You're not supposed to use the hooks directly.
By using
__FILE__, you ensure that it will work no matter what name is given to the plugin.Please re-open if this doesn't address your concern.