Ticket #5445 (closed defect: invalid)

Opened 10 months ago

Last modified 10 months ago

Add function to count number of functions hooked to an action

Reported by: pishmishy Assigned to: pishmishy
Priority: normal Milestone:
Component: General Version: 2.3.1
Severity: normal Keywords: has-patch plugin action hook
Cc:

Description

Add a function to count the number of functions hooked to a particular action. Useful to test if an action exists at all.

Attachments

count_action.patch (0.9 kB) - added by pishmishy on 12/10/07 10:14:11.
Adds count_action()

Change History

12/10/07 10:14:11 changed by pishmishy

  • attachment count_action.patch added.

Adds count_action()

12/10/07 10:14:43 changed by pishmishy

  • keywords set to has-patch plugin action hook.
  • status changed from new to assigned.

12/10/07 13:54:33 changed by darkdragon

There is already has_filter() and has_action() for testing whether or not a action or filter exists.

12/10/07 13:56:23 changed by pishmishy

  • status changed from assigned to closed.
  • resolution set to invalid.

12/10/07 14:05:55 changed by darkdragon

There are some issues with the patch

function count_filter($filter) {
    global $wp_filter;

    $i = 0;

    if( isset($wp_filter[$filter]) } {
        foreach($wp_filter[$filter] as $priority) $i += count($priority);
    }

    return $i;
}

function count_action($action) {
    return count_filter($action);
}
  • The global $wp_actions stores how many times an action was called.

And that is all. Really, for optimization, if it is not set, then it is better to return 0, like your patch. Not have an else condition. Then have the loop afterwards.

It would also be better to have some sort of caching once the number has been found.

You misunderstand. It would be useful to have this function, but just not for your second point. A guy (one guy) thought it would be useful. However, I'm not quite sure it would be useful for everyone else.

One practical application would be for debugging, "Did my hook actually add itself?"

12/10/07 14:12:30 changed by pishmishy

I'm afraid that I've got no real interest in pushing this function beyond my use in #5446. Thanks for pointing out has_action() :-)

12/10/07 21:16:27 changed by santosj

  • milestone deleted.