The functions.php file lets you add custom features and change how your WordPress site works without changing core files.
Functions.php role in Wordpress
<?php
// Add your PHP functions here
function my_custom_function() {
// Your code
}
add_action('init', 'my_custom_function');The file must start with <?php and contain only PHP code.
Use WordPress hooks like add_action or add_filter to connect your functions.
<?php
// Register a custom menu
function register_my_menu() {
register_nav_menu('header-menu', 'Header Menu');
}
add_action('init', 'register_my_menu');<?php // Add support for post thumbnails function add_theme_supports() { add_theme_support('post-thumbnails'); } add_action('after_setup_theme', 'add_theme_supports');
[year] shows the current year anywhere in your posts or pages.<?php
// Create a shortcode to display current year
function display_year() {
return date('Y');
}
add_shortcode('year', 'display_year');This code creates a shortcode [greeting] that shows a welcome message wherever you put it in your content.
<?php
// functions.php example
// Add a custom greeting shortcode
function custom_greeting() {
return 'Hello, welcome to my site!';
}
add_shortcode('greeting', 'custom_greeting');Always back up your functions.php before editing to avoid site errors.
Errors in functions.php can break your site, so test changes carefully.
Use child themes to add custom code without losing changes when updating the main theme.
functions.php is your themeโs place to add custom PHP code.
It helps you add features, change behavior, and create shortcodes.
Always use WordPress hooks to connect your functions safely.
Practice
functions.php file in a WordPress theme?Solution
Step 1: Understand the purpose of
This file is designed to hold custom PHP code that modifies or adds features to a WordPress theme.functions.phpStep 2: Compare with other options
Options A, B, and D describe unrelated tasks: database management (A), image storage (B), and URL control (D) are handled elsewhere in WordPress.Final Answer:
To add custom PHP code that changes or extends theme features -> Option AQuick Check:
functions.php = custom theme code [OK]
- Thinking functions.php stores images
- Confusing functions.php with database files
- Assuming it controls URLs directly
functions.php?Solution
Step 1: Recall WordPress function for menu registration
The correct function to register a navigation menu isregister_nav_menu().Step 2: Check the syntax
The syntaxregister_nav_menu('primary', 'Primary Menu');matches WordPress standards, while other options use incorrect function names.Final Answer:
register_nav_menu('primary', 'Primary Menu'); -> Option BQuick Check:
Menu registration uses register_nav_menu() [OK]
- Using non-existent functions like add_menu()
- Confusing register_nav_menu() with register_menu()
- Misspelling function names
functions.php:
function add_custom_text() {
echo 'Hello, visitor!';
}
add_action('wp_footer', 'add_custom_text');
What will happen on the website?Solution
Step 1: Understand the add_action hook
The code usesadd_action('wp_footer', 'add_custom_text');which runs the function at the footer of the site.Step 2: Analyze the function output
The functionadd_custom_text()echoes 'Hello, visitor!', so this text will show in the footer area on every page.Final Answer:
The text 'Hello, visitor!' will appear in the footer of every page -> Option CQuick Check:
add_action('wp_footer') adds output to footer [OK]
- Thinking text appears at top instead of footer
- Assuming function does nothing
- Confusing echo with return
functions.php but causes a fatal error:
function my_custom_function() {
echo 'Welcome!'
}
add_action('wp_head', 'my_custom_function');
What is the error and how to fix it?Solution
Step 1: Identify syntax error in PHP code
The echo statement lacks a semicolon at the end of the line, which causes a fatal syntax error.Step 2: Correct the syntax
Adding a semicolon after'Welcome!'fixes the error:echo 'Welcome!';Final Answer:
Missing semicolon after echo statement; add ; after 'Welcome!' -> Option AQuick Check:
PHP statements need semicolons [OK]
- Ignoring missing semicolon errors
- Changing hook unnecessarily
- Thinking echo is disallowed in functions.php
functions.php. Which code snippet correctly registers a sidebar widget area?Solution
Step 1: Recall the correct WordPress function for sidebar registration
The functionregister_sidebar()is used to register widget areas in WordPress themes.Step 2: Check the syntax of the function call
The correct usage passes an array with keys like 'name' and 'id' toregister_sidebar(). Other options use non-existent functions.Final Answer:
register_sidebar(array('name' => 'Footer Widget', 'id' => 'footer-widget')); -> Option DQuick Check:
Use register_sidebar() with array for widgets [OK]
- Using wrong function names like add_sidebar()
- Passing parameters incorrectly
- Confusing widgets with menus
Ready to prove it?
Pass the test and this pattern moves unknown
