Pre-requisite: Knowledge about what is Action and Filter. You can learn about it here.
Have you ever wanted to hook into a plugin, googled how to do it, but found no result?
Yes, incomplete/poor documentation is a common thing in WordPress development.
In this article, we will take a look at a plugin called Events Manager that has 100,000+ active installations but has no documentation about its Hooks.
Step 1: CTRL + SHIFT + F
Proper code editor like Sublime or VisualStudio Code has a functionality to search all files. Usually, the shortcut is CTRL (or ⌘ in Mac) + Shift + F.
So, open the plugin with your code editor and search for one of these: apply_filters
if you need to change a value or do_action
if you want to insert some codes at a certain place.
Step 2: Find What You Need
Scan through the filter names and see which one seems like the one you need. In my case, what I need is to add “IDR (Indonesian Rupiah)” to the Currency selection.
Hmmm, that filter named em_get_currencies
is suspicious. So I clicked on that to see the full code:
That’s definitely what I’m looking for!
Step 3: Hook into That Filter
You got the filter name and the parameter. Now, hook into that:
add_filter( 'em_get_currencies', function( $currencies ) {
$currencies->names['IDR'] = 'IDR - Indonesian Rupiah';
$currencies->symbols['IDR'] = 'Rp';
$currencies->true_symbols['IDR'] = 'Rp';
return $currencies;
} );
I opened the Events Manager settings and Voila! It’s there:
Conclusion
The example above will give you a general idea on how to customize a plugin.
It’s true that the hook you need might not exist. But if you use a plugin with decently high installations, you can expect it to have lots of hooks.
If it has low active installation, then it’s a risk to be using that plugin in the first place.
If you have experience dissecting a plugin, let me know how it goes in the comment below 🙂