Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
An Overview of WordPress Actions and Filters1:53 with Zac Gordon
WordPress development involves a lot of writing custom functions that hook into WordPress’s core functionality or operating procedure. Actions and filters allow us to determine exactly when WordPress will call our custom functions. This could be at any point from adding a user, to saving a post, or when the user logs into the admin area. Actions allow us to write functions that cause something to happen and filters allow us to manipulate and return content or data. Before moving on to looking at examples of actions and filters, we’ll take some time to go over how to these two essential functions.
[Zac Gordon] WordPress development involves a lot of writing custom functions
that hook in to WordPress' core functionality or operating procedure.
A hook is basically a conditional statement that you could add to your code
to see if anybody else has something that they'd like to run at that point.
For example, if we had a function called "hook example" we could say,
"If someone else has code to run, add it here" and then keep on going with our code.
Of course, actual hooks get a bit more complex than this,
but this basic concept will work for now.
WordPress has hooks like this embedded throughout its code
so that we could call our own functions at just about any time we want.
Hooks give plugins their power. Applications with no hooks make it really hard to add additional code.
WordPress hooks come in 2 forms: actions and filters.
Actions let us do something whenever WordPress does something that it has a hook for.
For example, we could say that when somebody publishes a post,
send out a tweet with a link to that post.
Filters, on the other hand, allow us to manipulate any data that WordPress provides a hook for.
For example, WordPress provides us a hook so that we can modify the main content of a post.
So, we could write a filter that takes post content and adds a "tweet this" button to the end of it.
As plugin developers, we have to understand the difference between actions and filters
so we could write good code that doesn't cause conflicts with other plugins.
To help us really understand this, let's look next at some examples of common actions and filters.
You need to sign up for Treehouse in order to download course files.Sign up