Keeping tabs on WordPress actions

If you’ve ever needed to see if an action has been fired and/or how many times it has been fired, there’s a handy function that will return the amount of times an action has been fired.

https://developer.wordpress.org/reference/functions/did_action/

This function can come in handy when building things that could be extended by other developers that may want to filter out something.

I noticed it use in the WP-Shortcake plugin (https://github.com/wp-shortcake/shortcake/blob/7c9f5483b9ca10d38fa9412b264a2e6d1e12d528/inc/class-shortcode-ui.php)

The plugin introduces a Shortcode Interface on post edit pages, but only if a specific shortcode ui has been registered.

To register a UI for a plugin, an array of settings must be passed to the “register_shortcode_ui” action, but if that’s not done, the Shortcake UI is never loaded, all because of the “did_action” check.

This is a handy way to make code extendable, but avoid unnecessary bloat.

Posted by Jason Bahl

Senior WordPress Engineer at 10up. I love problem solving and working with WordPress. I'm passionate about Open Source software and the web. When I'm not writing code, you'll find me spending time with my beautiful wife Rachel and my 2 sons Andrew and Blake. I also enjoy playing and watching soccer.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.