Nicola Mustone

Happiness Lead @ Automattic



Searching The Code

I’m sure that at least once you had to search something in the code, even if you are not a developer. Maybe a support technician told you that you can customize your site using WordPress actions, or you simply need to know the name of a function.

Well, searching into the code is not as easy as it seems, but I’ll try to explain here how I do it so that you can do it easily and quickly.

Coding knowledge are not required, but it would be very helpful if you can understand what you read.

Choose Your Editor

First of all, you need a code editor. There’s no way that you are going to search the code without a code editor.

Choose the one you prefer, there are many good editors, like Sublime Text or the free Atom.

I used both, but lately I’m using Atom.

Get The Code

Once you have your editor installed, download the code where you need to search, being it a plugin or a theme, or whatever it is, download it on your computer.

If your code editor has a project manager, create a project that has the folder you just downloaded. A thing that I find really helpful is to create a project for the folder wp-content when I’m working on a WordPress plugin or theme. This way I have access to all the plugins and themes I’m using, and it will be easier to cross-search in different locations.

Start Searching

Here comes the fun. There are mainly two ways to search the code:

  • With regular expressions
  • Without regular expressions

I’ll talk about the second first because it’s easier, but before that, here are some tips:

  • You don’t need to know exactly what you are searching about, but you need to have an idea.
  • Search with case-insensitive keywords, unless you know exactly what you are searching.
  • Be as restrictive as possible. Searching for the keyword function in a project is not a good idea. You are going to find too many results.
  • Search only where you need to search. If you have multiple folders in your project, don’t search in all of them. Search only where you think you can find good results.

Searching Without Regular Expressions

Let’s use the free plugin WooCommerce Stripe as an example, and let’s say that you need to find a filter, but you have no idea at all about the name of the filter. You though know what you need to do: add a field in the settings.

The first thing you would do is to search for all the filters in the plugin. You know that filters in WordPress are declared with the function apply_filters, so you will search for that.

Open the plugin folder or the project with the code editor and search in it (usually cmd + shift + f).

In the screenshot above, I’m searching for the keyword apply_filters in the folder plugins/woocommerce-gateway-stripe/ in my project. If I remove this second restriction I would search for the keyword in all the plugins, which is probably not a good idea.

Once you hit Enter, you start getting the results. In this case, I get 12 results.

Scroll through the results and try to find something related to what you are searching for, a filter for the settings.

In the image above you can see that the last result is the filter wc_stripe_settings, click on it to see it in the file and read the rest of the code.

Search With Regular Expressions

Searching with regular expressions is more powerful because it allows you to search for complex keywords without knowing exactly how is written what you are searching for.

I’m going to use the same example as before, but using regular expressions.
So, you know that the filter is declared with apply_filters and that you are searching for settings filter, so most probably the filter name will have the word settings in it.

Like before, search in the folder for this keyword: apply_filters(.*)?settings.

This time, the number of results is reduced to 1, which is exactly what you are searching for! But, what did we search for? What is that (.*)? in the keyword?

That is a regular expressions. apply_filters(.*)?settings means ”Search for anything that starts with apply_filters, has the word settings, and any number of any character in the middle of these two words”. I know, it’s not easy to understand, but it’s not easy to explain as well. It’s just easier if you try it and test.

I’d suggest you to learn more about regular expressions and how to use them here.

Happy Searching

As you can see from the two simple examples above, searching is easy if done correctly and with the proper tools. Like any other thing, it’s a matter of practice. Start searching properly, and you will understand how to best search for anything you need.

Happy searching!


Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 670 other subscribers

Leave a Reply

You Might Also Like These Articles


Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€1.00
€5.00
€10.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 670 other subscribers

Continue Reading

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading