How to Monitor PHP Memory Usage and Time Elapsed in WordPress Without a Plugin

Graphs of performance analytics on a laptop screen
Photo by Luke Chesser via Unsplash (No changes made)
Graphs of performance analytics on a laptop screen

While there are many good monitoring WordPress plugins available, sometimes programmers need a simple functionality to quickly see the metrics of time elapsed and PHP memory usage. This can be easily achieved with a simple PHP function, which we will show you how to add to your WordPress theme’s functions.php file.

Why Monitor PHP Memory Usage and Time Elapsed?

When it comes to optimizing WordPress sites, monitoring PHP memory usage and time elapsed is essential. PHP memory usage refers to the amount of memory that your site’s PHP scripts are using to execute, while time elapsed measures how long it takes for your site to generate and deliver content to users.

If your site takes a long time to load or uses too much memory, it can result in a poor user experience, low search engine rankings, and lost revenue. By monitoring these metrics, you can quickly identify and troubleshoot performance issues, and optimize your site for faster load times.

To monitor these metrics in WordPress, you can use various plugins such as Query Monitor, or Debug Bar. However, these plugins may add unnecessary overhead to your site, especially if you’re only interested in monitoring basic metrics like PHP memory usage and time elapsed.

In such cases, it’s much more efficient to use a simple function like sample_toolbar() to add a quick monitoring feature to your WordPress site. This function will use WordPress’ built-in global $wp_admin_bar object to add a new menu item to the admin bar, displaying the current PHP memory usage and time elapsed.

How to Add a Monitoring Function to Your WordPress Site

Step 1: Open the functions.php File of Your WordPress Theme

The first step is to open the functions.php file of your WordPress theme. This file contains all the functions that are used by your theme. If you’re using a child theme, make sure to open the functions.php file of your child theme.

Step 2: Add the Monitoring Function to Your functions.php File

Once you have opened the functions.php file, you can add the following code to the end of the file:


  
  
if ( ! function_exists( 'sample_toolbar' ) ) {
	/**
	 * Adds Toolbar info.
	 *
	 * @return void
	 */
	function sample_toolbar() {
		global $wp_admin_bar;

		$args = array(
			'id'    => 'sample-toolbar-memory',
			'title' => timer_stop( 0, 2 ) . ' s ' . size_format( memory_get_usage(), 2 ),
			'group' => false,
		);
		$wp_admin_bar->add_menu( $args );
	}
	add_action( 'wp_before_admin_bar_render', 'sample_toolbar', 999 );
}

This function creates a custom menu item on the WordPress admin toolbar that displays the elapsed time and memory usage for a page load. Here’s what each part of the function does:

  • function_exists(): Checks if the sample_toolbar() function already exists before creating it.
  • sample_toolbar(): Defines the sample_toolbar() function.
  • global $wp_admin_bar: Makes the $wp_admin_bar object available to the function.
  • $args: An array of arguments for the custom menu item.
  • id: The ID of the custom menu item.
  • title: The text to display in the custom menu item. The timer_stop() function returns the time elapsed since the start of the WordPress execution, and the size_format() function formats the memory usage.
  • group: Whether the custom menu item should be grouped with other items.

Step 3: Save the functions.php File

Once you have added the function to your functions.php file, you can save the file. The function is added to the wp_before_admin_bar_render hook, which is fired just before the admin bar is rendered on the page. The hook has a priority of 999, which ensures that the sample_toolbar() function is executed last, after all other functions that are hooked into the same event. This ensures that the memory usage and time elapsed values displayed in the admin bar are accurate.

Step 4: View the Monitoring Information in the WordPress Admin Toolbar

To view the monitoring information, simply login to your WordPress admin dashboard and look at the custom menu item that has been added to the WordPress admin toolbar. The custom menu item will display the elapsed time and memory usage for the current page load.

Conclusion

To sum up, monitoring PHP memory usage and time elapsed is crucial to ensuring the optimal performance of your WordPress site. By adding the sample_toolbar() function to your WordPress theme’s functions.php file, you can easily monitor these metrics and quickly identify and troubleshoot performance issues.

We hope this tutorial has been helpful in showing you how to add a simple monitoring functionality to your WordPress site without the need for a plugin. By regularly monitoring PHP memory usage and time elapsed, you can ensure that your site is performing at its best and providing a great user experience.