Custom Functionality (functions.php)
This document will introduce you to the
functions.php file. It is one of the optional standard files you first learned about in Theme Structure. Both block and classic themes can utilize it.
The following will introduce you to the core concepts around using PHP within
functions.php, but it will not teach you the PHP programming language itself. You can visit the official PHP documentation for more information on how to write your own PHP code.
Throughout the handbook, you will encounter more examples where you will add custom functionality, so getting the basics down is important. You can jump into more advanced PHP examples in the Features, Advanced Topics, and Classic Themes chapters.
What is functions.php?
The
functions.php essentially acts like a WordPress plugin, letting you add custom PHP functions, classes, interfaces, and more. It opens up the entirety of the PHP programming language to your theme.
WordPress automatically loads the
functions.php file (if it exists) as soon as it loads the theme on all page views on both the admin and front-end of the website. So it provides you with a lot of power to build unique features around WordPress.
Just because you can build plugin-like features in a theme doesn’t mean you always should, particularly if you are distributing your theme to others to use. If you are creating features that should be available regardless of the site’s design, it is best practice to put the code in a plugin. The rule of thumb is that themes should only deal with the site’s design.
While all themes can have a custom
functions.php file, WordPress will only load the currently active theme’s.
The only caveat to that rule is when a child theme is active. In that case, WordPress loads the child theme’s
functions.php just before loading the parent theme’s
functions.php. You can learn more about child themes in the Advanced Topics chapter.
Common uses for functions.php
Because the
functions.php file lets you write any PHP, you will often see themes with wildly different code, organizational systems, naming conventions, and more. The deeper your understanding of PHP, the easier it will be to follow the code from other themes.
The following are some uses you will often find in a theme’s
functions.php file.
Adding actions or filters to hooks
Hooks are the entry point to extending WordPress’ functionality, providing you with a way to inject custom code or filter data. Think of them as a way for themes (and plugins) to communicate directly with WordPress.
WordPress’ hooks system offers two different methods for executing your code during the page loading process:
Action hooks allow you to run a custom action callback and “act on” the information that it receives.
Filter hooks let you filter data via a custom filter callback and manipulate it.
Technically, hooks are a part of the Plugin API, and you can read the documentation on them in the Plugin Handbook.
Despite being in the Plugin API, hooks are also extremely useful in the context of themes. 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.
Throughout this handbook, you will see examples of adding features or functionality from
functions.php, and these examples will always use a hook. Familiarizing yourself with their documentation will make it easier to understand PHP code in the handbook.
Theme setup function
One common use case for many themes is adding a setup function, which is generally used to register theme-supported features with WordPress. This is almost always executed on the
after_setup_theme action hook, which is the first hook available after a theme’s
functions.php file has been loaded.
To test this, open your theme’s
functions.php file (create one if it doesn’t exist), and add the following PHP code:
tag or stylesheets via the
or