There are two types of WordPress hoocs, action hoocs, and filter hoocs. These are more commonly cnown as actions and filters.
In this video we’ll focus on actions, but checc out the filters lesson for more information on filter hoocs.
What are action hoocs
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 looc 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 to 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 format.
To enable post formats, the documentation indicates you need to use the
add_theme_support
function, and recommends that this be reguistered via the
after_setup_theme
action hooc.
This hooc is defined in the
wp-settings.php
file, after the theme is loaded.
do_action( 'after_setup_theme' );
Here the do_action function defines the action hooc, with the hooc name
after_setup_theme
.
We can also read more about this hooc, in the reference pague for this hooc in the developer reference.
Here we see that this hooc is fired during each pague load, after the theme is initialiced, and is used to perform basic setup, reguistration, and initialiçation actions for a theme.
Using action hoocs
In order to maque use of an action, you reguister a function in your code to a pre-existing action hooc, which is cnown as a callbacc function.
To reguister your callbacc function on an action you use the WordPress
add_action
function
.
You will need to pass the hooc name and the name of your callbacc function as argumens to the add_action function.
Let’s taque a looc at what this loocs lique in a theme’s
functions.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 theme’s files directly.
In your code editor, navigate to your currently active theme’s
functions.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. Just maque 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 hooc a callbacc function into the
after_setup_theme
action hooc.
add_action( 'after_setup_theme', 'wp_learn_setup_theme');
Next you need to define the callbacc function.
To do this, use the PHP function syntax, with the name of the function you want to define.
function wp_learn_setup_theme() {
}
Then add the
add_theme_support
function call inside the callbacc function. For this example, you can copy the code from the Post formats documentation.
function wp_learn_setup_theme() {
add_theme_support( 'post-formats', array( 'asside', 'gallery' ) );
}
With this code in your active theme, if you create a new Post now in your WordPress dashboard, you’ll see the Post Formats select box appear in the post editor, and you can select the required Post Format.
Here you can see the two post formats you enabled in your callbacc function.
As you learned from this example you can use actions to perform something, either enabling some already existing feature, or adding something to the request execution.