Divi Speedup: Stop Map Module Excerpts Loading Google Maps Scripts

Written by Dan Mossop

I recently noticed that the Divi Theme sometimes loads Google Maps, even when there are no maps in the page. This results in around 5 unnecessary requests on those pages. Preventing these requests will speed up the loading of these pages.

It turns out that the pages in question all showed an excerpt of a post containing a map module (or fullwidth map module). While the map module isn't displayed as part of the generated excerpt, the act of generating the excerpt fully processes the post and triggers the map module to load the scripts it requires – i.e. the Google Maps scripts.

Since the excerpts don't display a map, they don't need to load the scripts. Here's how to prevent this behavior and save around 5 requests on post listing pages.

Using Divi Booster to Stop Unnecessary Loading of Google Maps Scripts

I've added an option into Divi Booster to prevent excerpts from loading the Google Maps scripts unnecessarily.

It can be found on the Divi Booster settings page at "Site-wide Settings > Site Speed > Stop map module excerpts from unnecessarily loading maps scripts". Simply enable the option and save.

Once applied, pages which show excerpts of posts containing map modules should run that bit quicker.

The option is available in Divi Booster 2.7.7 onwards.

Using PHP to Stop Unnecessary Loading of Google Maps Scripts

If you'd prefer to go down the DIY route, you can use the following PHP code to achieve the same effect:
add_action('loop_start', 'dbc_loop_start');
add_action('loop_end', 'dbc_loop_end', 100);

// Suppress google map script loading by map modules
function dbc_loop_start($query) {
	if (dbc_map_modules_in_excerpts($query)) { 
		add_filter('et_pb_enqueue_google_maps_script', 'dbc_return_false');
	}
}

// Re-enable map script loading once loop done
function dbc_loop_end($query) {
	if (dbc_map_modules_in_excerpts($query)) { 
		remove_filter('et_pb_enqueue_google_maps_script', 'dbc_return_false');
	}
}

function dbc_map_modules_in_excerpts($query) {
	
	// Don't affect admin
	if (is_admin()) { return false; }
	
	// Don't affect visual builder
	if (!function_exists('et_core_is_fb_enabled') || et_core_is_fb_enabled()) { return false; }
	
	// Don't affect single posts
	if (is_singular()) { return false; }
	
	// Don't affect secondary queries
	if (!$query->is_main_query()) { return false; }
	
	// Don't affect Divi > Theme Options > General > Blog Style Mode, which shows full post content in loop
	if (!function_exists('et_get_option') || et_get_option('divi_blog_style', 'false') === 'on') { return false; }
	
	return true;
}

function dbc_return_false() { return false; }

Want get more out of Divi?

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

Boost Your Divi Site Performance with Divi Booster!

Speed up your Divi website and optimize page loading by preventing unnecessary Google Maps scripts from being loaded. Use Divi Booster, which offers a handy option to stop map module excerpts from loading the maps scripts. Discover this and hundreds of other features designed to enhance your Divi site.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest Posts

Hide the Page Title in the Hello Elementor Theme

Hiding the page title in the Hello Elementor theme allows you to create a cleaner and more customized page layout, free from default headings that may not fit your design. This is particularly useful for landing pages or custom content layouts where the default page...

How to Hide a Divi Module When Scrolling Up or Down

In this quick tutorial, I’ll show you how to hide a Divi module when scrolling in a specific direction (up or down), so that the module only shows when you want it to. This is especially useful when using Divi’s Scroll Effects feature and you have a effect you want to...

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Random Posts