Opened 7 months ago

Last modified 7 months ago

#22256 new enhancement

Hook namespacing — at Version 9

Reported by: scribu Owned by:
Priority: normal Milestone: Awaiting Review
Component: Plugins Version:
Severity: normal Keywords:
Cc: brian@…, xoodrew@…, info@…, knut@…

Description (last modified by scribu)

Currently, callbacks passed to add_action() and add_filter() are already sort of namespaced by the hook name, by priority and by the callback itself. You need to know all of them in order to use remove_action() successfully.

Acquiring the callback is especially problematic when it's:

  • an instance method, and you don't have access to the instance
  • a PHP 5.3 closure

Introducing namespaces for hooks, similar to jQuery's namespaced events, would not only take care of that problem, but also enable things like removing all callbacks added by plugin X, regardless of hook name or anything else.

Proposed syntax:

add_action( 'after_setup_theme:twentytwelve', function() {
  // do stuff
}

add_action( 'after_setup_theme:twentytwelve', function() {
  // do more stuff
}

add_filter( 'posts_clauses:p2p', function( $clauses ) {
  // do stuff
}

Later:

// remove specific callbacks added by the Twentytwelve theme
remove_action( 'after_setup_theme:twentytwelve' );

// remove all callbacks added by the P2P plugin
remove_action( '*:p2p' );

Source: http://core.trac.wordpress.org/ticket/22250#comment:14

Change History (9)

  • Description modified (diff)
  • Summary changed from Hook namespaceing to Hook namespacing
  • Description modified (diff)
  • Description modified (diff)
  • Cc brian@… added

Yes, I like this. I like this very much.

A problem with using dots is that some hooks will already have dots in their name. I don't know if this means another argument, or some other character or argument overloading, or doing this with a WP_Plugin class that has add_action etc methods.

  • Description modified (diff)

I know! Let's use the \ character as the namespace separator. </sarcasm-aimed-at-PHP-language-designers>

I haven't see any hook names with dots in them, though.

Anyway, #21883 looks interesting as well.

Hooks will often end up with a dot in them when they have a dynamic name, and frequently of a PHP file. For example, load-post-new.php.

  • Cc xoodrew@… added
  • Description modified (diff)

Oh yeah. Well, in the mean time, I replaced . with :.

Started a proof of concept over here: https://github.com/scribu/WordPress/compare/hook-ns/22256

Note: See TracTickets for help on using tickets.