Performance: Functions.php role
This affects the initial page load speed and server response time by controlling theme features and scripts loading.
Jump into concepts and practice - no test required
<?php
// functions.php
function theme_enqueue_scripts() {
if (is_front_page()) {
wp_enqueue_script('custom-script');
wp_enqueue_style('custom-style');
}
}
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
?><?php // functions.php wp_enqueue_script('jquery'); wp_enqueue_script('custom-script'); wp_enqueue_style('custom-style'); // No conditional loading or optimization ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unconditional script/style loading | Minimal | Multiple reflows if scripts manipulate DOM | High due to render-blocking CSS/JS | [X] Bad |
| Conditional and deferred loading | Minimal | Single reflow | Low paint cost due to fewer blocking assets | [OK] Good |
functions.php file in a WordPress theme?functions.phpfunctions.php?register_nav_menu().register_nav_menu('primary', 'Primary Menu'); matches WordPress standards, while other options use incorrect function names.functions.php:
function add_custom_text() {
echo 'Hello, visitor!';
}
add_action('wp_footer', 'add_custom_text');
What will happen on the website?add_action('wp_footer', 'add_custom_text'); which runs the function at the footer of the site.add_custom_text() echoes 'Hello, visitor!', so this text will show in the footer area on every page.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?'Welcome!' fixes the error: echo 'Welcome!';functions.php. Which code snippet correctly registers a sidebar widget area?register_sidebar() is used to register widget areas in WordPress themes.register_sidebar(). Other options use non-existent functions.