Theme Functions
The
functions.php file is where you add unique features to your WordPress theme. It can be used to hook into the core functions of WordPress to make your theme more modular, extensible, and functional.
What is functions.php?
The
functions.php file behaves like a WordPress plugin, adding features and functionality to a WordPress site. You can use it to call WordPress functions and to define your own functions.
There are advantages and tradeoffs to either using a WordPress plugin or using
functions.php.
A WordPress plugin:
- requires specific, unique header text;
- is stored in wp-content/plugins, usually in a subdirectory;
- only executes on page load when activated;
- applies to all themes; and
- should have a single purpose – for example, offer search engine optimization features or help with backups.
Meanwhile, a
functions.php file:- requires no unique header text;
- is stored in theme’s subdirectory in wp-content/themes;
- executes only when in the active theme’s directory;
- applies only to that theme (if the theme is changed, the features can no longer be used); and
- can have numerous blocks of code used for many different purposes.
With
functions.php you can:- Use WordPress hooks. For example, with the
excerpt_lengthfilter you can change your post excerpt length (from default of 55 words). - Enable WordPress features with
add_theme_support(). For example, turn on post thumbnails, post formats, and navigation menus. - Define functions you wish to reuse in multiple theme template files.
Theme Setup
A number of theme features should be included within a “setup” function that runs initially when your theme is activated. As shown below, each of these features can be added to your
functions.php file to activate recommended WordPress features.
It’s important to namespace your functions with your theme name. All examples below use
myfirsttheme_ as their namespace, which should be customized based on your theme name.
To create this initial function, start a new function entitled
myfirsttheme_setup(), like so:
1
2
3
4
5
6
7
8
9
10
| if ( ! function_exists( 'myfirsttheme_setup' ) ) :/*** Sets up theme defaults and registers support for various WordPress features** It is important to set up these functions before the init hook so that none of these* features are lost.** @since MyFirstTheme 1.0*/function myfirsttheme_setup() { |
Automatic Feed Links
Automatic feed links enables post and comment RSS feeds by default. These feeds will be displayed in
<head> automatically. They can be called using add_theme_support().
1
| add_theme_support( 'automatic-feed-links' ); |
Navigation Menus
You can set up multiple menus in
functions.php. They can be added using register_nav_menus() and inserted into a theme using wp_nav_menu(), as discussed later in this handbook. If your theme will allow more than one menu, you should use an array. While some themes will not have custom navigation menus, it is recommended that you allow this feature for easy customization.
1
2
3
4
| register_nav_menus( array( 'primary' => __( 'Primary Menu', 'myfirsttheme' ), 'secondary' => __( 'Secondary Menu', 'myfirsttheme' )) ); |
Each of the menus you define can be called later using
wp_nav_menu() and using the name assigned (i.e. primary) as the theme_location parameter.Post Thumbnails
Post thumbnails and featured images allow your users to choose an image to represent their post. Your theme can decide how to display them, depending on its design. For example, you may choose to display a post thumbnail with each post in an archive view. Or, you may want to use a large featured image on your homepage. While not every theme needs featured images, it’s recommended that you support post thumbnails and featured images.
1
| add_theme_support( 'post-thumbnails' ); |
Post Formats
Post formats allow users to format their posts in different ways. This is useful for allowing bloggers to choose different formats and templates based on the content of the post.
add_theme_support() is also used for Post Formats. This is recommended.
1
| add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) ); |
Initial Setup Example
Including all of the above features will give you a
functions.php file like the one below. Code comments have been added for future clarity.
As shown at the bottom of this example, you must add the required
add_action() statement to ensure the myfirsttheme_setup function is loaded.Content Width
A content width is added to your
functions.php file to ensure that no content or assets break the container of the site. The content width sets the maximum allowed width for any content added to your site, including uploaded images. In the example below, the content area has a maximum width of 800 pixels. No content will be larger than that.
1
2
| if ( ! isset ( $content_width) ) $content_width = 800; |
Other Features
There are other common features you can include in
functions.php. Listed below are some of the most common features. Click through and learn more about each of these features.- Custom Headers
- Sidebars (widget areas)
- Custom Background (needs link)
- Add Editor Styles (needs link)
- HTML5 (needs link)
- Title tag (needs link)
Comments
Post a Comment