Make WordPress Core

Opened 12 years ago

Closed 9 years ago

#22256 closed enhancement (wontfix)

Hook namespacing

Reported by: scribu's profile scribu Owned by:
Milestone: 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 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 (14)

#1 @scribu
12 years ago

  • Description modified (diff)
  • Summary changed from Hook namespaceing to Hook namespacing

#2 @scribu
12 years ago

  • Description modified (diff)

#3 @scribu
12 years ago

  • Description modified (diff)

#4 @rzen
12 years ago

  • Cc brian@… added

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

#5 @nacin
12 years ago

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.

#6 @scribu
12 years ago

  • 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.

#7 @nacin
12 years ago

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.

#8 @DrewAPicture
12 years ago

  • Cc xoodrew@… added

#9 @scribu
12 years ago

  • 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

#10 @toscho
12 years ago

  • Cc info@… added

#11 @scribu
12 years ago

The most likely problem that I can think of with : as a separator would be if plugins are using it in keys for get_option(), since we have a "pre_option_{$option}" hook.

A grep to that effect on the wp.org repos would be a big help in assessing the situation.

#12 @toscho
12 years ago

How about an exclamation mark (!) as separator or just a space?

#13 @knutsp
12 years ago

  • Cc knut@… added

I like the idea, but not the separator thing. Programming history is packed with failed string separators, like the space on a command line, commas, semicolons and so on. Such will some day create the need for quoting and we will always have to parse the string. Soon we have to

add_action( '"some_colon:action":"pre:fix"', array() )

To avoid that, introduce

wp_add_action( $name, $object, $function, $prefix )

as an alias and then deprecate add_action in the long run.

#14 @chriscct7
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Zero interest in a long time on this. Closing as wontfix

Note: See TracTickets for help on using tickets.