WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#22256 new enhancement

Hook namespacing — at Version 1

Reported by: scribu Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords:
Focuses: Cc:

Description (last modified by scribu)

Currently, callbacks passed to add_action() and add_filter() are already sort of namespaced by the hook name, by the callback itself, by priority and by argument count.

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( 'parse_query.p2p', function() {
  // 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' );

Change History (1)

comment:1 @scribu2 years ago

  • Description modified (diff)
  • Summary changed from Hook namespaceing to Hook namespacing
Note: See TracTickets for help on using tickets.