The critical rendering path is how a browser shows your webpage on screen. Optimizing it helps your site load faster and look good quickly.
Critical rendering path optimization in Wordpress
/* Example: Defer non-critical JavaScript in WordPress */ function defer_parsing_of_js($url) { if (is_user_logged_in()) return $url; // Don't defer for logged in users if (FALSE === strpos($url, '.js')) return $url; if (strpos($url, 'jquery.js')) return $url; // Don't defer jQuery return "$url' defer='defer"; } add_filter('script_loader_tag', 'defer_parsing_of_js', 10, 1);
This example shows how to add defer to JavaScript files to delay their loading until after the page shows.
Optimizing the critical rendering path often means loading only what is needed first, then loading other parts later.
/* Inline critical CSS in header.php */ <style> /* Only styles needed to show above-the-fold content */ body { margin: 0; font-family: Arial, sans-serif; } header { background: #333; color: white; padding: 1rem; } </style>
/* Load main stylesheet asynchronously */ function load_css_async() { echo "<link rel='preload' href='style.css' as='style' onload='this.rel=\'stylesheet\''> <noscript><link rel='stylesheet' href='style.css'></noscript>"; } add_action('wp_head', 'load_css_async');
/* Move scripts to footer in functions.php */ function move_scripts_to_footer() { remove_action('wp_head', 'wp_print_scripts'); add_action('wp_footer', 'wp_print_scripts'); } add_action('wp_enqueue_scripts', 'move_scripts_to_footer');
This WordPress plugin code does three things: it defers JavaScript loading except jQuery, inlines small critical CSS in the header, and loads the main stylesheet asynchronously. This helps the page show content faster.
<?php /* Plugin to optimize critical rendering path */ function crp_optimize_scripts($tag, $handle, $src) { // Defer all scripts except jQuery if (strpos($src, 'jquery.js') === false) { return '<script src="' . $src . '" defer></script>'; } return $tag; } add_filter('script_loader_tag', 'crp_optimize_scripts', 10, 3); function crp_inline_critical_css() { echo "<style>body{margin:0;font-family:sans-serif;}header{background:#222;color:#fff;padding:1rem;}</style>"; } add_action('wp_head', 'crp_inline_critical_css'); function crp_load_css_async() { echo "<link rel='preload' href='" . get_stylesheet_uri() . "' as='style' onload='this.rel=\'stylesheet\''> <noscript><link rel='stylesheet' href='" . get_stylesheet_uri() . "'></noscript>"; } add_action('wp_head', 'crp_load_css_async'); ?>
Always test your site after optimization to make sure scripts and styles still work correctly.
Inlining too much CSS can make your HTML heavy; keep it small and only for above-the-fold content.
Defer or async scripts carefully; some scripts need to load early to work properly.
Critical rendering path optimization helps your site load and display faster.
Load only essential CSS and scripts first, defer others.
Use WordPress hooks to control how and when styles and scripts load.
Practice
Solution
Step 1: Understand the purpose of critical rendering path optimization
This optimization focuses on loading the most important parts of a webpage first to improve speed.Step 2: Compare options with this goal
Adding plugins or increasing image size slows loading, and disabling caching is counterproductive.Final Answer:
To make the site load faster by prioritizing important content -> Option CQuick Check:
Optimization = Faster loading important content [OK]
- Thinking more plugins always improve speed
- Confusing image quality with loading speed
- Ignoring caching benefits
Solution
Step 1: Identify the hook for managing scripts
wp_enqueue_scripts is the standard hook to add or modify scripts and styles in WordPress.Step 2: Understand deferring JavaScript
Using wp_enqueue_scripts allows adding attributes like 'defer' to scripts to delay loading.Final Answer:
wp_enqueue_scripts -> Option BQuick Check:
Defer scripts with wp_enqueue_scripts [OK]
- Using wp_head which runs too early for deferring scripts
- Using wp_footer without enqueuing scripts properly
- Confusing init hook with script loading
function inline_critical_css() {
echo '<style>.header { color: blue; }</style>';
}
add_action('wp_head', 'inline_critical_css');
What will this code do when the page loads?Solution
Step 1: Analyze the function output
The function prints a <style> block with CSS for '.header' inside the <head> section.Step 2: Understand the effect on page load
Inline CSS in the head applies styles immediately, so header text will be blue on load.Final Answer:
It adds the CSS inside the page header to style the header text blue immediately -> Option AQuick Check:
Inline CSS in head = immediate style [OK]
- Thinking it loads external CSS file
- Assuming CSS is deferred or delayed
- Confusing echo output with syntax error
function defer_scripts($tag, $handle) {
return str_replace(' src', ' defer src', $tag);
}
add_filter('script_loader_tag', 'defer_scripts', 10, 2);
What is the main issue with this code?Solution
Step 1: Understand the filter's effect
The code adds 'defer' to every script tag by replacing ' src' with ' defer src'.Step 2: Identify the problem with deferring all scripts
Some scripts must load early (like jQuery), deferring them breaks functionality.Final Answer:
It adds 'defer' to all scripts, including those that must load early -> Option DQuick Check:
Deferring all scripts = breaks early-loading scripts [OK]
- Thinking it's a syntax error
- Assuming defer is never added
- Believing src attribute is removed
Solution
Step 1: Combine inlining CSS and deferring JS properly
Inlining critical CSS in the head ensures fast style application; deferring only non-essential JS avoids blocking rendering.Step 2: Evaluate other options
Loading all files normally misses optimization; deferring all JS or removing files breaks functionality or style.Final Answer:
Inline critical CSS in the <head> using a hook and add 'defer' attribute only to non-essential scripts via wp_enqueue_scripts -> Option AQuick Check:
Inline CSS + defer non-critical JS = best practice [OK]
- Deferring all scripts causing errors
- Not inlining critical CSS
- Removing essential files
Ready to prove it?
Pass the test and this pattern moves unknown
