I haven't ever seen a tag such as do_action('something_10'). Is it invalid if I use numbers?

If the method is valid, how can I write different numbers in the tag string conditionally? For example, is the following code correct($grade is the number)?


2 Answers 2

  1. When you "hook"/add_action/*_filter('whatever'); a callback function to do_action('whatever');, then you basically add the function (or object-method) name to the global $wp_filters-array.
  2. Doing so, you add the function/method name to an array that is built like the following

    $wp_filter[ $tag ][ $priority ][ $idx ]
    // $tag = action/filter name
    // $priority = 3rd argument / execution order
    // $idx = "unique" name
  3. Not the 3rd argument/$idx is built using the _wp_filter_build_unique_id() function, that takes the first args from add_action/*_filter().
  4. Inside this function, every function name stays the same, only method names change. This is the reason, why you often find "funky" method names, prefixed with a pretty long number.

Can I add numbers after letters in do_action tag?

So yes, numbers are completely valid when naming action hooks. Summed up, it's save to use a-zA-Z0-9_ as function/method/variable names. - is not supported. And while some characters might work well on your system, it mostly depends on the encoding if your chosen function name (example: _wUT?a_nice_DÄY!()) works or not. WordPress itself does not check if a function/var/hook/filter/whatever name is valid or not.

Addition to @BrianFegter answer about "contextual hooks".

You can see some "contextual hooks", when you take a look at the »help«-panel in (any) screen in your (MU or single) installation, using this plugin1).

enter image description here enter image description here

1) Plugin inspired by @StephenHarris article.


Yes, you can dynamically create your own action hooks. WordPress has several dynamic hooks like admin_head-hookname and admin_footer-hookname. new_grade_$n is a valid hook name.

  • Thanks! Then, should I use "_$n" or "-$n" ?
    – Jenny
    Sep 14, 2012 at 1:21
  • @Jenny Whatever your preference is. :) Sep 14, 2012 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.