Functions.php is the “brain” of a WordPress theme and the interface between how WordPress uses the theme to present a page to the viewer. It is a power and yet dangerous tool in that the PHP functions there control how things display using local definitions, rather than the site’s underlying data.
What functions.php Is
In a WordPress theme, functions.php exists to provide the theme with useful visual PHP functions: small bits of work that change how the site displays in defined ways.
Presentational changes don’t alter the site’s underlying data: things like post content, registered users, custom post types, taxonomies such as tags and categories, or sitewide data like “site title” or “customer discount codes.”
To know whether a function qualifies, ask yourself: “If I changed themes, would I lose data, or would things just display differently?” If you’d lose data (a post type would disappear, you’d lose your customer discount codes, etc.), then you’ve strayed from the presentational role of themes.
Thus references such as 3rd party links, are not a suitable location for implemenation in functions.php from a theme level and a NEVER to be modified by a WordPress site developer. Why? the minute you update that theme, all your changes, links, images and references are removed.
functions.php Autoloads, Before the Rest of the Theme
Functions.php is the gateway between WordPress and the theme display elements. Thus after Wordpress loads, functions.php follows in the hierarchy, and has the potential of over-riding default WordPress presentational elements.
This can be seen in the chart below, where the theme functions.php is loaded in step 6.
Uses of functions.php
- Theme support for featured images
- Theme support for custom image sizes
- Theme navigation menu.
This block looks complicated if you don’t understand WordPress hooks and the
wp_enqueue_() functions, but it boils down to the following statement: “On every page, we want to load the files
page-min-height.js, and the theme’s own
style.css,” plus instructions for how to find those files.
An important design rule to follow, is while it may be simple to quickly just modify functions.php to enable some function that just does not seem to run in a custom plugin, the reality is, it endangers the portability and upgrade-ability to any theme. Thus keep the links local, rather than referencing any 3rd party scripts and code. A better design strategy is to port the code to a local reference.
- Functions.php is a specially named PHP file that can be located inside any WordPress theme. WordPress knows to execute the code in functions.php during its normal PHP processing.
- functions.php ‘s job is to add custom functionality to the theme and site. It is composed of PHP functions—small bits of PHP code that add or change functionality across the site.