WordPress - Display hook action priority in the dashboard


If your WordPress site has lots of plugins, it's sometimes difficult to keep track of what is manipulating your content. Ever wondered what priority all your various actions and filters have? This is a widget which will show you which actions are registered to your blog's hooks, and their priority order.

It looks like this:

List of actions with various priorities.

Stick this code in your theme's functions.php or in its own plugin.

PHP PHPfunction edent_priority_dashboard_widget_contents() {
    global $wp_filter;
    //  Change this to the hook you're interested in
    $hook_name = "the_content";
    if ( isset( $wp_filter[$hook_name] ) ) {

        //  Display the hook name in the widget
        echo "<h3>{$hook_name}</h3>";

        //  Start a list
        echo "<ul>";

        //  Loop through the callbacks in priority order
        foreach ( $wp_filter[$hook_name]->callbacks as $priority => $callbacks ) {
            echo "<li>Priority: {$priority}<ul>";

            foreach ( $callbacks as $callback ) {
                //  Some callbacks are arrays
                if ( is_array( $callback["function"] ) ) {
                    if (is_object($callback["function"][0])) {
                        $callback_info = get_class($callback["function"][0]) . '::' . $callback["function"][1];
                    } else {
                        $callback_info = $callback["function"][0] . '::' . $callback["function"][1];
                    }
                } else {
                    $callback_info = $callback["function"];
                }
                //  Show the information
                echo "<li>Callback: {$callback_info}</li>";
            }
            echo "</ul></li>";
        }
        echo '</ul>';

    }
    else {
        echo "No filters found for hook: {$hook_name}";
    }

    //  Scrap of CSS to ensure list items display properly on the dashboard
    $priority_css_code = "#edent_dashboard_widget ul { list-style: circle; padding: 1em; }";
    //  Inline the CSS
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"data:text/css;base64," .
        base64_encode($priority_css_code) . "\">";

}

//  Register the widget with the admin dashboard
function edent_register_dashboard_widget() {
    wp_add_dashboard_widget(
        "edent_dashboard_widget",   //  ID of the widget
        "Priorities",   //  Title of the widget
        "edent_priority_dashboard_widget_contents"  //  Function to run
    );
}
add_action( "wp_dashboard_setup", "edent_register_dashboard_widget" );

Why?

WordPress lets you add actions and filters to hooks. For example, whenever your blog wants to show some content, a hook of the_content is run.

You can add an action to run a function when that happens. For example, if you want to make all the text in your blog posts uppercase, you could add this to your theme or plugin:

PHP PHPfunction lower_case_everything( $content ) {
   return strtolower( $content );
}
add_filter( 'the_content', 'lower_case_everything', 99 );

The add_filter says "When the hook called the_content is fired, run the function lower_case_everything, with a priority of 99". The lower the number, the sooner the function is run.


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

2 thoughts on “WordPress - Display hook action priority in the dashboard”

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">