WordPress Theme Development Tutorial Manual - Theme functions.php
functions.php
DocumentationThe only place where we can add functionality to our WordPress theme. In it we can mount custom features to the core WordPress functionality, making our theme more modular, more extensible, and more feature-rich.
What is functions.php?
functions.php
File behaves like a WordPress plugin to add some features and functionality to a WordPress website. We can use it to call WordPress functions or define our own.
functions.php
can produce the same results. If we're creating new features for a theme, whether the sitebeWhat kind of. It's better to put them in a plugin TheThere are advantages and disadvantages to using a WordPress plugin or functions.php:
Use the WordPress plugin:
- Requires specific, unique title text.
- Stored in wp-content/plugins
- is executed only when activated.
- Applies to all themes
- There should be a specific purpose - for example, to provide search engine optimization features or help with backups.
utilization functions.php
Documentation:
- Unique title text is not required; the
- Stored in the themes subdirectory of wp-content/themes.
- Only executed when the theme is active; the
- Applies only to this theme (if the theme changes, these features are no longer available)
- There can be many blocks of code that implement various functions
Each theme has its own function file, but it is only activated when the theme'sfunctions.php
The code in the file will be executed directly. If our theme already has a function.php file, we can add code to it. If not, we need to create the file first.
Subthemes can have their own functions.php
file. Adding functions to a child theme's function file is a risk-free way to modify the parent theme. When updating the parent theme, we don't have to worry about adding new functions that will disappear.
functions.php
In the parent theme's functions.php
before loading, but he will not overwrite the files of the parent theme. Child Themes functions.php
can be used to expand or replace the functionality of the parent theme. At the same time, the functions.php
When loadingAny plugin files loaded after Theexist functions.php
in which we can:
- Use WordPress hooks. For example, use the
excerpt_length
filter, we can change the article excerpt length (default is 55 words). - utilization add_theme_support() Functions enable WordPress features. For example, enable thumbnails, post formatting and navigation menus.
- Defines functions that need to be reused in other template files of the theme.
functions.php content example
Below are some examples that we can use in our functions.php file to support various features. We can use any of these examples in our own theme if we choose to submit it to the WordPress.org theme directory.
Theme Setting
Many theme functions should be included in the "Settings" function, which is run when the theme is activated. As shown below, we can add these functions to our theme's functions.php
file to activate the recommended WordPress features.
myfirsttheme_
Used as a namespace, we can define it according to our theme name.To create this initial function, we need to create a new function called myfirsttheme_setup() , as shown below:
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* Setting theme defaults and registering some WordPress functions
* It's important to set up these functions before the init hook to make sure they don't fail
* @since MyFirstTheme 1.0
*/
function myfirsttheme_setup() {
Note: In the example above, the function myfirsttheme_setup is open but not closed. Be sure to close our function
Automatic Feed Links
We can use the add_theme_support() function adds automatic-feed-links support to the theme, which by default publishes post and comment RSS feeds. these feeds will be published in the<head>
Automatic display .
add_theme_support( 'automatic-feed-links' ).
navigation menu
customizablenavigation menuAllows users to edit and customize menus in the menu management panel, providing users with a drag-and-drop interface to edit various menus in the theme.
We can use the register_nav_menus() function in the functions.php
Set up multiple menus in . And use the wp_nav_menu() Add it to the theme by inserting , if our theme allows multiple menus, you can use arrays. Although some themes do not have customizable navigation menus, it is recommended to enable this feature so that users can easily manage the site menu.
register_nav_menus( array(
'Primary' => __( 'Primary Menu', 'myfirsttheme' ),
'secondary' => __( 'Secondary Menu', 'myfirsttheme' )
) );
Each of the menus we have defined can be accessed using the wp_nav_menu() call, calling the menu style, specifying the menu name name (e.g., primary) as the function's theme_location
The parameter value is sufficient.
Load Text Field
By making the strings in the theme translatable, the theme can be translated into multiple languages. To do this, we must use theload_theme_textdomain() function to set up translation files. For more information on making topics available for translation, read theinternalizationPart.
load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' ).
Article Thumbnails
Article thumbnails and featured imagesAllows users to choose an image to represent their post. We can decide how to display them based on the theme design. For example, we may choose to display article thumbnails for each article on the archive page. Or, we may want to use a large featured image on the homepage. While not every theme requires featured images, it is recommended that thumbnail support be added to the theme.
add_theme_support( 'post-thumbnails' ).
Article Formatting
Article FormattingAllow users to format their posts in different ways. This is useful for allowing site administrators to choose different formats and templates depending on the content of the post. add_theme_support()
It can also be used to enable article formatting.
add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) ;)
Learn more about article formatting.
Content width
We can add a content width variable to thefunctions.php
file to ensure that site content or files do not break the site layout. Content Width sets the maximum allowable width of any content allowed to be added to our website, including uploaded images. In the example below, the maximum width of the content area is 800 pixels. Content displayed on the site will not exceed this width.
if ( ! isset ( $content_width) )
$content_width = 800;
Other Functions
We can also find out more about this in thefunctions.php
Other common features are included . Here are some of the most common.
- Customized Title
- Sidebar (Widget Area)
- Customized backgrounds
- Adding Editor Styles
- HTML5
- Title Tags
functions.phpDocument templates
If we include all the functions listed above, the following code this is our theme for the functions.php'sThe
/**
* :: Theme functions and definitions
*
* @package MyFirstTheme
* @since MyFirstTheme 1.0
*/
/**
* First, the tool theme design and style sets the maximum width, which will limit the size of the uploaded file and the width of the embedded content
*/
if ( ! isset( $content_width ) )
$content_width = 800; /* pixels */
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* Setting theme defaults and registering some WordPress functions
* It's important to set these functions before the init hook to make sure they don't fail
*/
function myfirsttheme_setup() {
/**
* Make the theme translatable, the translation files are in the /languages/ directory.
*/
load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );
/**
* Add default post and comment feeds to the
*/
add_theme_support( 'automatic-feed-links' );
/**
* Add thumbnail and featured image support
*/
add_theme_support( 'post-thumbnails' ); /** * Add support for thumbnails and featured images */.
/**
* Add two custom menus
*/
register_nav_menus( array(
'Primary' => __( 'Primary Menu', 'myfirsttheme' ),
'secondary' => __( 'Secondary Menu', 'myfirsttheme' )
) );
/**
* Enable support for the following post formats: aside, gallery, quote, image, and video
*/
add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) ); ;
}
endif; // myfirsttheme_setup
add_action( 'after_setup_theme', 'myfirsttheme_setup' ); }