Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
WordPress Hooks (Actions and Filters) Search Tool

WordPress Hooks (Actions and Filters) Search Tool

December 20, 2013 PHP Jesin A 5 Comments

wordpress hooks search tool thumbnail

This started out as a PHP regex experiment. I wanted to find the WordPress “action” hooks in a particular theme so that I can add my own action function to it.

So instead of searching through the whole lot of files for do_actions I sat down writing a regular expression to do this. I was very satisfied with the results which made me think about creating a tool to do this.

So here you go a standalone PHP script which seeps through the PHP code of the WordPress core, plugins, themes or anything you specify and gets you a list of actions and filters.

I use the word “standalone” because this isn’t a WordPress plugin or anything which requires WordPress functions. This is a script which uses a regex in preg_match_all() to find all “tag” names in the do_action() and apply_filters() function. This does not even require WordPress, if you want to find the hooks in a plugin just download the plugin, unzip it to a directory readable by this tool.

Download WordPress Hooks Search Tool | GitHub

Usage

Upload the code to your document root (or anywhere else) and access it from the browser.

http://<yourdomain>/wp-hooks-search.php

Enter the directory or file to search for hooks. It can be either relative or absolute. If a directory is entered all subdirectories are also scanned. If you wish to scan the current directory enter a dot (.) in the text box.

Note: On Windows (WAMP and XAMPP users) you must use forward slash in the path instead of the usual backslash like this

wordpress/wp-content/themes/customizr

The second textbox is optional. If you are searching for a particular name in the hook you can enter it there.

Screenshots

wordpress hooks search in twentyfourteen
Listing out actions and filters in the Twenty Fourteen Theme
wordpress hooks search keyword search
Searching for a hook by entering its name. Notice the dot in the first text box. It makes the tool search from the current directory

Regular Expression

Even if this tool has 250+ lines of code the hero of this script is the regular expression which hooks out the hooks 😀

/(apply_filters|do_action)\s*\(\s*[\'\"](.+?)\s*[,\)]/

A do_action or an apply_filters function can look like any of the following.

do_action('wp_my_action',$variable);
do_action  ( "wp_head"  );
apply_filters('filter_name',  $value);
apply_filters('filter_' . $variable,  $value);

Here is the anatomy of the regular expression.

(apply_filters|do_action) – Match the word “apply_filters” OR “do_action”

\s* – The \s matches a whitespace character and putting an asterisk (*) in front of it matches zero or more of whitespaces. This match has been placed in several places because the function could be anything like these.

do_action('wp_my_action',$variable);
do_action( 'wp_my_action', $variable );
do_action  ('wp_my_action',$variable  );

\( – Matches the opening brackets of the function.

[\’\”] – Match single quotes OR double quotes. Functions may use single or double quotes around their parameters so this matches any.

(.+?) – This is the portion that matches the first parameter of the function i.e. the hook, .+ matches one or more characters while the question mark ? does a non greedy match. Read more about regex greediness here.

[,\)] – This matches a comma OR the closing brackets. The second argument is optional in the do_action() function is optional so a comma matches if there is a second parameter like this.

do_action( 'wp_my_action', $variable );

If there is only one parameter the brackets match.

do_action( 'wp_head' );

So the comment form is all yours send me your bouquets and brickbats on this script

Related posts:

wordpress custom 403 error pageCustom 403 and 401 error pages in WordPress php category thumbnailHow to use PHP to minify HTML output php category thumbnailRedirecting WordPress archives to a page php category thumbnailPHP Sessions Tutorial web design category thumbnailJavascript Validation with Regular Expressions

Tags: ajax, php scripts, wordpress

Comments

  1. Lee says

    February 2, 2014 at 12:31 am

    I like this script. Works well in my test environment. Not sure when I will use it but I do know I will and it will save me lots of time. Thank you.

    Reply
  2. Lee says

    April 13, 2014 at 12:20 am

    Same Lee as above. Just letting you know I’ve used this script many times since first download. Will write a post about the script soon and hopefully send you a few visitors.

    You might or might not have thought about this, but a really neat feature would be to make it possible to view the function a filter/hook is found within. Maybe use a ‘magnifying glass’ icon next to the filter/hook then load a new tab/modal when the icon is clicked.

    Reply
  3. Glenn Kelley says

    September 22, 2014 at 5:45 am

    Amazing addition to our toolset. This allows us to find issues within themes and sites on our network – allow for us to understand better what a theme and/or application is doing and allows us to react to our customers requests for support. We are a hosting company – and their code is their responsibility but we try to help where we can …

    I used this to chase down an issue with a Christian News Media Outlets theme.
    I was able to drop their load times from over 54 seconds to now just 2.4 seconds

    Amazing tool – Keep up the good work – I would have purchased it … 🙂

    Reply
  4. Anh Tran says

    September 22, 2014 at 7:15 am

    One small question: how can I update the script when a new version of WordPress is released? Or will it work with any version?

    Thanks.

    Reply
  5. John Rom says

    September 22, 2014 at 1:07 pm

    For the sake of completeness, you might want to add the rare but useful “do_action_ref_array” to the regex.

    /(apply_filters|do_action(?:_ref_array)?)\s*\(\s*[\'\"](.+?)\s*[,\)]/
    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.