Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    WordPress – measure load time TTFB

    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.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT
    Understanding the IAB’s Global Vendor List (GVL)
    • Dev Tips and Tricks

    Understanding the IAB’s Global Vendor List (GVL)

    December 6, 2023 by createIT
    Effortlessly transform JSON data into a clear schema
    • Uncategorized

    Effortlessly transform JSON data into a clear schema

    December 5, 2023 by createIT
    Anatomy of WordPress malware: a technical deep dive
    • Dev Tips and Tricks
    • Technology

    Anatomy of WordPress malware: a technical deep dive

    December 4, 2023 by createIT
    Initializing CMP with the Stub Script and cmpapi.js
    • Dev Tips and Tricks

    Initializing CMP with the Stub Script and cmpapi.js

    November 29, 2023 by createIT

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

    Contact us