WordPress – measure load time TTFB - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    WordPress – measure load time TTFB

    September 2, 2022
    Last update: January 3, 2024
    2 min read
    10
    0
    0
    WordPress – measure load time TTFB

    CHALLENGE: My WP pages suffer from long load times

    SOLUTION: Add a PHP function to measure Time to first byte

    Having a fast website is really important. WordPress sites with good performance will be positioned higher in Search Results Rankings, and visitors will also be happier. TTFB (Time to first byte) is an important performance measurement. It’s wasted time – the time between the initial request and server response. After typing a website url or clicking a link – the server needs to do a lot of things to prepare the response data (fetch content, process PHP functions, query data from database). The first byte received by the browser will be our TTFB Time. Also you can check our complex article about fast tips to speed up WordPress.

    Measure TTFB

    There are multiple online tools for checking Time to first byte for your website, e.g.: GTmetrix. You can also use the Chrome ‘Dev Tool’ (Network tab – click the first request on the list, go to the ‘Timing’ tab – Waiting (TTFB) is the value we’re looking for ). The disadvantages of using these tools are:

    • they measure TTFB only for a particular url, for one moment in time
    • they do not show the entire time history of all users visiting your website
    • they connect from one external server, measuring only one network latency

    Slow WordPress TTFB

    My WordPress website started to load slowly. What is the reason? In most cases – some additional WP plugin has been activated. Some plugins use inefficient database queries or define slow PHP functions. These will increase TTFB significantly. It’s good practice to use only well-established/popular plugins and minimize the number of activated ones. As a digital marketing agency, we can help you provide SEO-compliant solutions to your website.

    Measure WP load time

    We’re going to add a PHP function to measure and save every page load time to a text file. On every request WordPress will save the load time to: /wp-content/uploads/ct-load-time86234.txt

    // Grab the page load time upon WordPress shutdown.
    function ct_page_load_time() {
        $ignoreThisRequest = false;
        if (strpos($_SERVER['REQUEST_URI'] , 'robots.txt') !== false) {
            $ignoreThisRequest = true;
        }
        if (! is_404() && !$ignoreThisRequest ) {
            // echo '<p>Page load time: '.timer_stop(0, 5).' seconds.</p>';
            $page_load_time = timer_stop(0, 5) . ' seconds';
            // put data to file:
            $time = date("Y-m-d H:i:s", time());
            $ban = "#$time" . " @@ " . $_SERVER['REQUEST_URI'] . " || " . $page_load_time . "\r\n";
            $upload_dir = wp_upload_dir();
            $file = trailingslashit($upload_dir['basedir']) . 'ct-load-time86234.txt';
            $open = fopen($file, "a");
            $write = fputs($open, $ban);
            fclose($open);
        }
    }
    add_action( 'shutdown', 'ct_page_load_time' );

    Analyze TTFB

    Now we can analyze the list and look for slow subpages/shortcodes that slow down the website. If a particular subpage has significantly higher TTFB Time – there might be inefficient shortcode added, one that uses a lot of server resources. Poorly written plugins often include slow PHP functions and slow database queries.

    Interested in enhancing your site’s performance? Learn more by exploring our article on the async plugin WordPress.

    Find slow DB queries

    We already have a list of subpages with long waiting time. To determine if the reason of bad performance is slow DB queries, we can use the <a href=”https://wordpress.org/plugins/query-monitor/”> Query Monitor plugin. It’s a tool for debugging db queries. It displays all necessary information in the admin toolbar. Slow queries will be highlighted using red color.

    That’s it. Make sure you follow us for other useful tips and guidelines.

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us