Welcome to this lesson on creating custom functionality for block themes.
With this lesson, you will describe the purpose of a WordPress theme's functions.php file,
decide when to use a plugin instead of the functions.php file when developing a theme,
and list some common uses of the functions.php file for extending the functionality of WordPress.
So let's dive in.
What is the purpose of a WordPress theme's functions.php file?
Adding features or functionality to your theme can be accomplished by adding the optional functions.php file
introduced in the beginner WordPress developer learning pathway.
Do check out the lesson on theme structure.
We can choose to add the functions.php file in order to extend WordPress without changing the files
by using the WordPress hook system.
To learn more about WordPress hooks, you can refer to the module found within the beginner developer learning pathway.
Like plugins, you should always run your code on a hook so that it performs its functionality at the appropriate point in the load process.
For example, you could add various theme support features after your theme is initialized by WordPress
by using the after setup theme action hook and the add theme support function.
Here's a look at the functions.php file included in the default 2024 theme.
The register block pattern category function registers the page's pattern category for us here.
Note that WordPress Core already provides us with many pattern categories.
It just so happens that this particular pattern category is not provided, so the theme is doing it for us.
This will run the 2024 pattern categories function when the init hook is executed.
How do you know when to use a plugin instead of the functions.php file?
Since you can add plugin-like features to your theme, it's important to use this power wisely.
If you plan to distribute your theme, you'll want to consider placing your code within a plugin rather than relying on the functions.php file.
And this way, a website owner's site won't break if they decide to replace their theme.
If you plan to submit your theme to the WordPress.org theme directory, you'll want to be sure to follow the guidelines there.
Let's take a look at some common uses of the functions.php file for extending functionality of WordPress.
For classic themes, you could use the functions.php file to add block theme styles.
If you take a look at the 2021 theme, you'll see an example of this where they add theme support for block styles to this classic theme.
You can also load scripts and styles with the functions.php file, which we'll be covering in the next lesson on including assets.
To see an example of this, though, you can take a look at the 2022 theme.
And finally, you can choose to include other PHP files by using the functions.php file.
So you can refer to the 2022 theme again for an example of this, and you'll find that it uses the block patterns.php file that's located in the ink folder.
This is done by adding require, get template directory, and concatenating the file path.
You can learn more about adding custom functionality by referring to the theme handbook available to you on WordPress.org.