WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 6 years ago

#4389 closed enhancement (invalid)

Improve plugin activate and deactivate hooks

Reported by: Otto42 Owned by:
Milestone: Priority: normal
Severity: major Version:
Component: Optimization Keywords:
Focuses: Cc:

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)

comment:1 follow-up: @markjaquith8 years ago

  • Milestone 2.4 deleted
  • Resolution set to invalid
  • Status changed from new to closed

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.

comment:2 @JeremyVisser8 years ago

That still doesn't address the issue of renaming a plugin before deactivating it, or does it?

comment:3 @mwdmeyer8 years ago

Well WordPress isn't going to load the plugin if you've changed the name of it.

comment:4 @markjaquith8 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.

comment:5 in reply to: ↑ 1 @Otto428 years ago

  • Resolution invalid deleted
  • Status changed from closed to 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?

comment:6 @Otto428 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Nevermind. Found the call to plugin_basename().

Note: See TracTickets for help on using tickets.