Make WordPress Core

Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5445 closed defect (bug) (invalid)

Add function to count number of functions hooked to an action

Reported by: pishmishy's profile pishmishy Owned by: pishmishy's profile pishmishy
Milestone: Priority: normal
Severity: normal Version: 2.3.1
Component: General Keywords: has-patch plugin action hook
Focuses: 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 (1)

count_action.patch (939 bytes) - added by pishmishy 17 years ago.
Adds count_action()

Download all attachments as: .zip

Change History (7)

@pishmishy
17 years ago

Adds count_action()

#1 @pishmishy
17 years ago

  • Keywords has-patch plugin action hook added
  • Status changed from new to assigned

#2 @darkdragon
17 years ago

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

#3 @pishmishy
17 years ago

  • Resolution set to invalid
  • Status changed from assigned to closed

#4 @darkdragon
17 years ago

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?"

#5 @pishmishy
17 years ago

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() :-)

#6 @santosj
17 years ago

  • Milestone 2.5 deleted
Note: See TracTickets for help on using tickets.