There are two types of wordpress hooks, action hooks and filter hooks.
These are more commonly known as actions and filters.
In this video we'll focus on actions, but check out the filters lesson for more information
on filter hooks.
As their name states, actions allow you to perform some action at a specific point during
the execution of a wordpress request.
To better explain this, let's look at an example.
When developing a wordpress theme, it's possible to enable support for different post formats.
You can read more about post formats in the Advanced Administration section of the wordpress
developer documentation.
Post formats are a way to allow users with access to create posts on a wordpress site and
choose from a predefined list of formats.
Depending on the chosen post format, a different template layout can be rendered, displaying
the post in a different way.
To enable post formats, the documentation indicates that you need to use the Add Theme Support
function and recommends that this be registered via the After Setup Theme Action Hook.
This hook is defined in the wpsittings.php file after the theme is loaded.
Here the Do-Action function defines the action hook, but the hook name of After Setup Theme.
You can also read more about this hook in the reference page for this hook in the developer
reference.
Here we see that this hook is fired during each page load, after the theme is initialized
and is used to perform basic setup, registration and initialization actions for a theme.
In order to make use of an action, you register a function in your code to a pre-existing action
hook, which is known as a callback function.
To register your callback function on an action, you use the WordPress Add Action function.
You will need to pass the hook name and the name of your callback function as arguments
to the Add Action function.
Let's take a look at what this looks like in a themesfunctions.php file.
For this lesson, you're going to write some code inside your currently active theme.
Please note that this is for demonstrative purposes only.
Unless you are developing the theme yourself, it is not recommended to edit an existing
themes files directly.
In your code editor, navigate to your currently active themesfunctions.php file and open it.
If your theme doesn't have a functions.php file, you can create one in the root of your
theme directory.
Let's make sure it's named functions.php and has the opening PHP tag at the top of
the file.
Then, add the following code to your functions.php file to hook a callback function into the
AfterSetupTheme action hook.
So Add Action AfterSetupTheme is the action name and WP Learn SetupTheme will be the callback
function.
Next you need to define the callback function.
To do this, use the PHP function syntax with the name of the function that you want to define.
FunctionWP Learn SetupTheme and the function is created.
Then use the Add theme support function call inside your callback function.
For this example, you can copy the code from the post formats documentation.
With this code in your active theme, if you create a new post now in your WordPress dashboard,
you'll see the post format select box appear in the post editor.
And you can select the required post format from the two that you enabled.
As you can see from this example, you can use actions to perform something, either enabling
some already existing feature or adding something to the request execution.