The best way to customize systems on WordPress is to hook your code into execution via action and filter hooks. Usually, available hooks can be found by viewing the developer docs for the plugin or theme you are customizing.
This is not always helpful, however. Sometimes you cannot find the right hook for the job. Other times, the documentation is unclear and you are not sure how to make use of a hook. Additionally, you don’t know exactly the hook’s context. “What exactly is it? The hook name seems like what I’m looking for, so I guess I’ll give it a shot.”
A few hours, error_log()
‘s, and var_dump()
‘s later, you have finally discovered that the hook wasn’t what you’re needing. This can make simple customizations take an unnecessary amount of time. Sometimes, this guessing game workflow can even make a customization impossible for you all because the hook you need simply wasn’t listed in the docs. You’re forced to strike out before even getting in the game.
Finding and utilizing hooks should not be such a mystery. You may be a master at PHP and WordPress customizations, but you’re missing one very crucial skill. Stop depending on outdated, unclear, and incomplete developer docs. It’s time to head to the source (code)!
Introducing grep
If you’ve never used the command line before, it should definitely be next on your list of things to learn. And how convenient that I’m about to teach you the most important command you will probably use on a Unix system: grep
.
grep
is a shell command that searches for pattern matches in given input files. More specifically, you’re able to recursively search files in bulk by specifying a regex pattern or fixed string to search. As it finds matches, grep
will output the line containing the match and which file the match was found. Are angels singing in your head yet like they were for me?
Common grep
Commands
I’m so excited to be the one sharing this with you today because this really is the beginning of the command line pocket of your web developer tool belt. You are going to go from a PHP/WordPress Master to a PHP/WordPress Expert. Seriously, it is a total game changer when it comes to your capabilities and work efficiency. Now’s the time to overcome your fear of the command line!
To start, cd
into the directory that you would like to search. This could be the root of a plugin, theme, or even a fresh download of WordPress if you’re searching for WordPress Core hooks. You’re now ready to start grep
‘ing! In my sample outputs, I’m using WooCommerce version 3.9.0.
Simple Recursive Search
grep -FR "do_action" .
./includes/abstracts/abstract-wc-data.php: do_action( ‘woocommerce_before_’ . $this->object_type . ‘_object_save’, $this, $this->data_store );
./includes/abstracts/abstract-wc-data.php: do_action( ‘woocommerce_after_’ . $this->object_type . ‘_object_save’, $this, $this->data_store );
./includes/abstracts/abstract-wc-order.php: do_action( ‘woocommerce_before_’ . $this->object_type . ‘_object_save’, $this, $this->data_store );
…
List All Hook Uses
grep -ER "do_action|apply_filters" .
…
./templates/myaccount/my-account.php:do_action( ‘woocommerce_account_navigation’ ); ?>
./templates/myaccount/my-account.php: do_action( ‘woocommerce_account_content’ );
./templates/myaccount/my-address.php: $get_addresses = apply_filters(
…
List Hooks with Keyword
grep -ER "do_action\s?\(\s?[\'\"].*checkout.*[\'\"][^\(\)]*\)" .
Exclude Files by Pattern
grep -FR --exclude="./includes/*" --exclude="*/.*" --exclude="*/assets/*" "do_action" .
./packages/woocommerce-blocks/src/Domain/Bootstrap.php: do_action( ‘woocommerce_blocks_loaded’ );
./packages/woocommerce-blocks/src/RestApi/Controllers/Cart.php: do_action( ‘woocommerce_add_to_cart’, $cart_id, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
./packages/woocommerce-blocks/src/RestApi/StoreApi/Utilities/CartController.php: do_action( ‘woocommerce_add_to_cart’, $cart_id, $product_id, $request[‘quantity’], $variation_id, $request[‘variation’], $request[‘cart_item_data’] );
…
Specify Files by Pattern
grep -R --include="*.php" "checkout" .
./packages/woocommerce-blocks/src/Domain/Bootstrap.php: do_action( ‘woocommerce_blocks_loaded’ );
./packages/woocommerce-blocks/src/RestApi/Controllers/Cart.php: do_action( ‘woocommerce_add_to_cart’, $cart_id, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
./packages/woocommerce-blocks/src/RestApi/StoreApi/Utilities/CartController.php: do_action( ‘woocommerce_add_to_cart’, $cart_id, $product_id, $request[‘quantity’], $variation_id, $request[‘variation’], $request[‘cart_item_data’] );
…
Counting Matches per File
grep -FRc "apply_filters" .
…
./includes/abstracts/abstract-wc-product.php:27
./includes/abstracts/abstract-wc-session.php:0
./includes/abstracts/abstract-wc-settings-api.php:3
./includes/abstracts/abstract-wc-shipping-method.php:11
…
Counting Total Hook Uses
grep -ER "do_action|apply_filters" . | wc -l
2682
Adding Context
grep -FR -A 5 "do_action( 'woocommerce_after_add_to_cart_quantity' )" .
grep -FR -B 10 "do_action( 'woocommerce_after_add_to_cart_quantity' )" .
grep -FR -C 3 "do_action( 'woocommerce_after_add_to_cart_quantity' )" .
Conclusion
Isn’t it great how many ways you can search for hooks using grep
? And who would’ve thought there’s over 2,500 hook uses in WooCommerce 3.9.0!
Now you can work more efficiently by finding every hook in any open source WordPress system. Read the source code that’s made available to you and no longer be limited to only what has been documented.