What Is functions.php and How to Use It in WordPress
functions.php, or the theme functions file, is an important file used by WordPress themes. It works like a small plugin and is loaded automatically both in the admin panel and on the front-end pages of the site. It is usually used to add functions, classes, actions and filters that apply within the theme. Through this file you can extend the theme’s functionality and change how WordPress behaves.
Where functions.php Is Located and How to Use It
The functions.php file is located in your theme’s folder:
In it you can use built-in WordPress functions, add your own PHP functions and hook into core actions and filters. Although every installed theme has its own functions.php file, only the one that belongs to the active theme works. If your theme does not have this file for some reason, you can simply create a text file named functions.php and place it in the theme directory.
Child themes can also have their own functions.php files. They are used to supplement or override the functionality of the parent theme. This lets you add changes cleanly without interfering with the main theme’s source code.
What You Can Do with functions.php (with Examples)
The functions.php file lets you extend the theme’s functionality and add your own functions, hooks and filters. Below are the main tasks and examples of how to implement them.
Enabling thumbnail support:
add_theme_support( 'post-thumbnails' );
Adding new post formats:
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'quote' ) );
Creating navigation menus:
register_nav_menus( array(
'main_menu' => 'Главное меню',
'footer_menu' => 'Меню в футере',
) );
Loading your own scripts and styles:
function my_theme_scripts() {
wp_enqueue_style( 'main-style', get_stylesheet_uri() );
wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/js/custom.js', array('jquery'), null, true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
Automatically adding text to the end of every post:
function add_custom_text($content) {
if (is_single()) {
$content .= '<p>Спасибо за чтение! Подпишитесь на обновления.</p>';
}
return $content;
}
add_filter('the_content', 'add_custom_text');
Creating your own widget:
class Welcome_Widget extends WP_Widget {
function __construct() {
parent::__construct('welcome_widget', 'Приветственный виджет');
}
function widget($args, $instance) {
echo $args['before_widget'] . '<p>Добро пожаловать на наш сайт!</p>' . $args['after_widget'];
}
}
add_action('widgets_init', function() {
register_widget('Welcome_Widget');
});
Registering a custom post type:
function create_portfolio_post_type() {
register_post_type('portfolio',
array(
'labels' => array('name' => 'Портфолио'),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
}
add_action('init', 'create_portfolio_post_type');
What to Put in functions.php and What in a Plugin
functions.php is ideal for:
- functions closely tied to the theme and its visual side,
- loading styles, scripts, menus, thumbnails, post formats, widgets, custom shortcodes and theme hooks.
A plugin is better when:
- the functionality must work independently of the theme (for example, SEO, caching, social network integrations, contact forms),
- you want to keep the functionality when you change themes,
- the function is complex or shared by several sites (it is better to move it into a plugin for reuse).
Tips for Working with functions.php
- Use a child theme to make changes so that updates to the main theme do not erase your functions.
- Keep the amount of code in functions.php to a minimum — if the functionality is large or universal, it is better to build a plugin.
- Always test added functions on a local or staging site so you do not break the live site.
- Use hooks (actions and filters) so that you do not modify the WordPress core and thereby stay compatible with future updates.
Conclusion
The functions.php file remains a key tool for adding PHP functions and extending theme functionality. It lets you configure the site flexibly, load styles and scripts, and add widgets, shortcodes and custom post types.
Our specialization is WordPress website development and maintenance. Contact us for a free consultation — [email protected], +371 29394520
