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.
    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
    1 response

    Add comment

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

    Popular news

    Automating stock and price updates in WooCommerce
    • Dev Tips and Tricks

    Automating stock and price updates in WooCommerce

    September 23, 2024 by createIT
    Integrating advanced subscription features in WooCommerce
    • Dev Tips and Tricks

    Integrating advanced subscription features in WooCommerce

    September 16, 2024 by createIT
    Fetching Time records from ActiveCollab API
    • Dev Tips and Tricks

    Fetching Time records from ActiveCollab API

    September 9, 2024 by createIT
    Docker Compose for PrestaShop
    • Dev Tips and Tricks

    Docker Compose for PrestaShop

    September 2, 2024 by createIT
    WordPress wizard in admin – step by step
    • Dev Tips and Tricks

    WordPress wizard in admin – step by step

    August 29, 2024 by createIT
    Order Status Sync between PrestaShop and External APIs
    • Dev Tips and Tricks

    Order Status Sync between PrestaShop and External APIs

    August 26, 2024 by createIT
    What is PHP used for in web development 
    • Dev Tips and Tricks

    What is PHP used for in web development 

    August 22, 2024 by createIT
    Automating WooCommerce product availability date
    • Dev Tips and Tricks

    Automating WooCommerce product availability date

    August 15, 2024 by createIT
    WP Quiz Adventure – FAQ
    • Dev Tips and Tricks

    WP Quiz Adventure – FAQ

    August 12, 2024 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