Igor Benić Profile picture
Feb 14 18 tweets 6 min read
I am embarrassed to say, but I didn't understand hooks at first.

Started fully understanding hooks when I started working on my custom #WordPress plugins.

I'll try to teach you everything about them in this thread. 👇 💻
At first, the WordPress Hook system was also called the Plugin API, because it's the core system that allowed extending WordPress.

This is the old documentation: codex.wordpress.org/Plugin_API

The new one is a little better at explaining: developer.wordpress.org/plugins/hooks/
Let's talk about actions. When you start developing, you'll most likely find code snippets that use add_action().

Reference: developer.wordpress.org/reference/func… Image
So, you are registering a function that will be called when an event (action) occurs.

You provide the priority of it and how many arguments from that even you expect.

Why is priority useful?
If you depend on a plugin, and they use the same action but set the priority to 9 and save data.

Your function that is set to a priority of 10 can get that data from the database.
How such actions (events) are triggered?
They are done with do_action().

Reference: developer.wordpress.org/reference/func…

Let's consider a real action 'user_register'. This action is triggered when a user has been created.

Reference: developer.wordpress.org/reference/hook…
Let's now see how we can use that. We can hook into that action to perform something else.

If we just need the user ID, we don't need to define arguments (default: 1). If we also need other data, we would set 2. Image
Let's now talk about Filters. Those are used in general to change data before it will be used for something.

When starting out, you've probably seen code snippets with add_filter().

Reference: developer.wordpress.org/reference/func…

There is a big difference between filters and actions:
Functions registered to actions don't have to return anything.

Functions registered to filters are required to return a value.
As actions have do_action() to trigger hooked functions.

Filters use apply_filters().

Reference: developer.wordpress.org/reference/func…
Let's cover a simple filter 'the_title' from get_the_title():
developer.wordpress.org/reference/func…

We expect from get_the_title to return a title. If we don't return a value with our function, there will be an error. That's the difference we explained above.
When using add_filter, we can decide to accept the post ID or just the title. Let's say that we want to get the date from it and put it in the title.

And we need to make if there is nothing retrieved from ID, just return the provided title. Image
Now that we covered how to hook our functions to filters and action, what about removing them?

Sometimes, customizing a site can involve removing already registered actions and filters.

When calling functions to remove them, you need to specify the exact priority as well.
Those functions are:
- remove_action: developer.wordpress.org/reference/func…
- remove_filter: developer.wordpress.org/reference/func…

You specify which hook, function, and priority to remove.
Have this in mind:
- function won't be called if an action/filter was already triggered

Example:
You are hooking into 'woocommerce_init'.
But you are doing that much later and WooCommerce was initialized already.
It won't work.
- if functions are registered through a class, they won't be removed by just putting the function name.

add_action( 'init', [ $object, 'the_function' ] );
remove_action( 'init', 'the_function'); ❌
remove_action( 'init', [ $object, 'the_function'] ); ✅
Another thing:
When hooking into a filter/action that potentially calls the same, remove your before or you'll end up in an infinity loop.

Fix:
1. Inside our function, remove the action/filter
2. Process code
3. Re-hook our function Image
I hope you've learned something from this thread.

And if you have someone who is starting out with WordPress, retweet them the first tweet :D

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Igor Benić

Igor Benić Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @igorbenic

Feb 16
What's your struggle when developing with or extending WooCommerce?

Do you miss something from their documentation?

Did you know they have a useful Wiki on their Github repository?

I've listed a few of their Wiki docs in this thread. 🧵
When working with WooCommerce, you might need to query products.

For that, you can use wc_get_products or WC_Product_Query.

Check various examples: github.com/woocommerce/wo…
Another thing I do a lot is working with Orders.

To query them you can use wc_get_orders and WC_Order_Query

Examples: github.com/woocommerce/wo…
Read 14 tweets
Feb 11
I firmly believe anyone can learn how to develop with WordPress.

The problem is, there are only a couple of resources for beginners. And it can become overwhelmed.

That's why I compiled this helpful list for you.

👇 Thread:
Plugins are one of the best things about WordPress.

When developing plugins, you learn a lot about WordPress as well. So I would focus on it first.

Check the Plugin Handbook:

developer.wordpress.org/plugins/
Next up are themes. They are used to define how your site looks and work on the front.

The classic themes are still used a lot, so definitely learn how such themes work.

developer.wordpress.org/themes/
Read 12 tweets
Jan 17
I investigated a bit the success of Potion.so. Mostly because I am thinking of building in public. So let’s see how @noahwbragg is building in public and after only 10 months of opening the product to public, he went full-time. #buildinpublic
@noahwbragg This thread is really long, so if you want to see all his tweets on Potion, you can use this in the Twitter search (and replace YEAR-MONTH-DAY): (Potion OR SaaS OR Notion OR saas) (from:noahwbragg) until:YEAR-MONTH-DAYsince:YEAR-MONTH-DAY -filter:replies
@noahwbragg TL;DR (1 / 2) In short, here is what @noahwbragg does:

- Share ups and downs while building the product
- Uses YouTube to showcase features or talks about behind the scenes
- Showcasing how others are using Potion or what can be built with it
Read 73 tweets
Jan 17
In ~6 hours, I'll publish a set of 60 tweets about how @noahwbragg built @potion_so and went full-time. Before that, do know that he is also a co-host of the podcast @pjourneypod with @digitaltrouble. Here is a list of episodes that mention Potion! Chronologically. #buildinpublic
"Coming to the Idea for Potions"
share.transistor.fm/s/22e9f619

"Simplifying Goal Tracking"
share.transistor.fm/s/0cf65200

"Feeling Overwhelmed"
share.transistor.fm/s/4545914a

"Many Open Threads"
share.transistor.fm/s/6b7fb4b5

"Launching Early Access"
share.transistor.fm/s/e0fb4b85
"Dealing with Competition"
share.transistor.fm/s/79fd258f

"Getting the value of Twitter"
share.transistor.fm/s/d112b0fa

"Stair Stepping Back"
share.transistor.fm/s/400962cd

"How the last 6 months have felt"
share.transistor.fm/s/be24e6ad

"Finding Channels and Getting Coins"
share.transistor.fm/s/42afecde
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(