WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 4 months ago

Last modified 4 months ago

#33886 closed enhancement (duplicate)

Need ability to determine the priority at which an action hook or filter is being run

Reported by: bobbingwide Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: Plugins Keywords:
Focuses: Cc:

Description

I have a trace routine which can be invoked for any hook. It can also be registered to run at multiple priorities. I'm trying to use it to see what's happening during 'the_content' processing. It would be nice if the output was able to show the priority of the actual invocation.

Current solution

In this first iteration my trace function calls the following.

/**
 * Find the current priority
 * @return string priority
 */
function bw_trace_inspect_current() {
  global $wp_filter;
  $tag = current_filter();
  $current = current( $wp_filter[ $tag ] );
  bw_trace2( $current, "current", false, BW_TRACE_VERBOSE );
  $priority = $current[ 'bw_trace_results']['accepted_args'];
  $priority = substr( $priority, 2 );
  return( $priority );
}

This super hack relies on the fact that you can define accepted args as a decimal value So I append the priority to the $accepted_args parameter. I'm using a base value of 9 , since I want to trace all the passed parameter values. It seems to be high enough. Note: I can't guarantee I can find what it should be for the given action hook or filter.

wp-content/plugins/oik-bwtrace/includes/bwtrace-actions.php(338:64) bw_trace_inspect_current(2) 338 2015-09-15T17:53:33+00:00 1.801802 0.001625 cf=genesis_loop,genesis_entry_content,the_content 27 1463 32065360/32341352 F=300 current Array
(
    [capital_P_dangit] => Array
        (
            [function] => capital_P_dangit
            [accepted_args] => 1
        )

    [do_shortcode] => Array
        (
            [function] => do_shortcode_earlier
            [accepted_args] => 1
            [replaced] => do_shortcode
        )

    [bw_trace_results] => Array
        (
            [function] => bw_trace_results
            [accepted_args] => 9.11
        )

)

Aside: As you can see the trace output is also fairly useful. It tells me that if I wanted to inspect the result between capital_P_dangit and do_shortcode I'd have to fiddle even more than I've already done with my bw_replace_filter() function... which has added the [replaced] entry.

Preferred solution

WordPress to provide an API that I can call to return the current hook priority without me having to mess with internal structures.

I don't need the whole stack, just the priority of the end on in $wp_current_filter.

Change History (10)

#1 @bobbingwide
3 years ago

  • Summary changed from Need ability to determine the priority which an action hook or filter is being run at to Need ability to determine the priority at which an action hook or filter is being run

In this example the priority is 11.

Last edited 3 years ago by bobbingwide (previous) (diff)

#2 follow-up: @SergeyBiryukov
3 years ago

has_filter( current_filter(), __FUNCTION__ ) returns the priority.

#3 in reply to: ↑ 2 @bobbingwide
3 years ago

Replying to SergeyBiryukov:

has_filter( current_filter(), __FUNCTION__ ) returns the priority.

This returns the priority of the first invocation, not the current one. My routine can be attached to the same hook with multiple priorities. e.g. the_content:11,the_content:10

So for priority 10, with verbose tracing I could see the results after these filters have been applied

wp-content/plugins/oik-bwtrace/includes/bwtrace-actions.php(333:64) bw_trace_inspect_current(1) 339 2015-09-25T09:10:13+00:00 1.695575 0.024268 cf=genesis_loop,genesis_entry_content,the_content 27 17898 31566984/32127896 F=293 current Array
(
    [convert_smilies] => Array
        (
            [function] => convert_smilies
            [accepted_args] => 1
        )

    [convert_chars] => Array
        (
            [function] => convert_chars
            [accepted_args] => 1
        )

    [prepend_attachment] => Array
        (
            [function] => prepend_attachment
            [accepted_args] => 1
        )

    [bw_trace_results] => Array
        (
            [function] => bw_trace_results
            [accepted_args] => 9.10
        )

)

#4 @lgedeon
2 years ago

I think this might get what you need.

<?php
function current_filter_priority() {
        global $wp_filter;
        return key( $wp_filter[current_filter()] );
}

#5 @bobbingwide
2 years ago

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

@lgedeon Thanks Luke. I'll do a bit more testing and then either mark this as 'wontfix' or convert your snippet into a patch.

#6 @netweb
2 years ago

  • Milestone Awaiting Review deleted

This ticket was mentioned in Slack in #core by bobbingwide. View the logs.


4 months ago

#8 @bobbingwide
4 months ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

This enhancement was delivered as a fix to #39007, which was raised after WP_Hook was delivered in 4.7.0. Closing as worksforme.

Last edited 4 months ago by bobbingwide (previous) (diff)

#9 @bobbingwide
4 months ago

  • Resolution set to worksforme
  • Status changed from reopened to closed

#10 @SergeyBiryukov
4 months ago

  • Resolution changed from worksforme to duplicate

Duplicate of #39007.

Note: See TracTickets for help on using tickets.