WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#5445 closed defect (bug) (invalid)

Add function to count number of functions hooked to an action

Reported by: pishmishy Owned by: 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 6 years ago.
Adds count_action()

Download all attachments as: .zip

Change History (7)

pishmishy6 years ago

Adds count_action()

comment:1 pishmishy6 years ago

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

comment:2 darkdragon6 years ago

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

comment:3 pishmishy6 years ago

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

comment:4 darkdragon6 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?"

comment:5 pishmishy6 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() :-)

comment:6 santosj6 years ago

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