Tracing WordPress Actions and Filters

Cal Evans, Developer Advocate Reading estimate: 3 minutes

Ever wonder exactly what is going on inside WordPress? As with any PHP script, you have a lot of good options for debugging and tracing what is going on. However, if all you want is a quick dump of the actions and filters that WordPress is calling, there is a very easy way to do this.

Props: I was inspired to write this plugin during a talk Helen Hou-Sandi gave. I believe it was at WordCamp NYC. Thanks Helen.

In the talk she mentioned an action 'all' that I'd not yet come across.

add_action( 'all', ... )

Setting the action hook as described above will add your "..." function to every hook WordPress calls.

Since I was fighting with the iThemes Membership plugin at that time, trying to find the appropriate hook to take a specific action, I decided to experiment with what she mentioned. Two bottles of rum later, and with the help of this plugin, I did find the appropriate hook, but that's a blog post for a different day.

The Easy Way

First of all, as with most things, I have grossly over-engineered this solution. If you just want a down and dirty solution, Andrew (@nacin) Nacin published the absolute simplest form of this idea ever in his post titled "5 Ways to Debug WordPress".

add_action( 'all', create_function( '', 'var_dump( current_filter());' ) );

If you add this line to your functions.php and then open a page on your WordPress powered site, you will see every action and filter that is called. Of course it will be inline in all of your template and content and that may not be the best solution.

The Fun Way

This is all fine and good but I wanted something that didn't interfere with my content. I'm sure had I spent more than 10 seconds searching the Plugin Repository, this has been done before. However, I already have a closet full of wheels that I've reinvented, what's one more?

I created a GitHub project called wp-action-trace. I did not and most likely will not submit this to the WordPress Plugin Repo because it would have a very narrow audience. I would hate for this to get installed by accident on someone's WordPress-powered site and slow things down unexpectedly.

The instructions for use are very simple:

  1. Install the plugin.
    (This is a test. If you do not know how to install a plugin manually, this plugin is not for you).
  2. Activate the plugin through WordPress
  3. Open a page in your WordPress-powered site
  4. Add showDebugTrace=1 to the URL (put a ? or & before it as appropriate)
  5. Scroll to the bottom of the page to see the results

There are 3 flags that control the output:

  • showDebugTrace=1
    This turns the display on. If you have the plugin installed and activated but don't put this on the URL, nothing will show. The plugin is still active, and will still slow your site down, but it will not display anything.
  • showDebugTime=1
    This will force the plugin to show you the time - as generated by microtime() - that the action was executed.
  • showDebugArgs=1
    This will force the plugin to display the arguments that the plugin was called with, if any.

Setting showDebugTrace=1 on the query string will give you the simplest output. It is just a list of the actions and filters.

Additionally, if you look at the code, there are a couple of actions/filters that I've filtered out of the list. You can add as many action/filters to this array as you want to help filter out the ones you aren't interested in. Alternatively, you can remove the two I've added and see everything.

Only for Development

As mentioned in the README, this plugin is not for production. This means that if you do not have one already, you need to clone your WordPress-powered website to a development platform before installing this plugin.

Of course, if your WordPress powered site is running on the Pantheon platform, we've got you covered. From your dashboard you can pull everything down from production, add this plugin in, activate it, and see everything that is running. Just make sure and delete it from development before you push to test, or at a minimum never activate it there.

If you don't yet have a Pantheon account, you can get one for free and play with this—and any other WordPress plugin you want. Take two minutes and register. Within 10 minutes you can have a WordPress development environment set up and operational.

Hopefully this will help you as it helped me. If you aren't currently plagued with the problem of figuring out the exact filter or action to hook in to, you can still use this plugin just to see everything that is going on under the hood of your site. Either way, I hope it helps.

Share

Discover More

Safely Publish to Web from Google Docs with Pantheon Content Publisher

Roland Benedetti (Senior Director, Product) and Zack Rosen (Co-Founder)
Reading estimate: 7 minutes

Unifying Content and Code: Inside Pantheon’s Vision for Content Operations

Chris Yates
Reading estimate: 5 minutes

How Pantheon Protects Your Site from Software Supply Chain Risks in Open Source

Steve Persch
Reading estimate: 8 minutes

Try Pantheon for Free

Join thousands of developers, marketers, and agencies creating magical digital experiences with Pantheon.