Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active April 28, 2019 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save glueckpress/56c88900aad391b95b5d44f4e752a1a4 to your computer and use it in GitHub Desktop.
Save glueckpress/56c88900aad391b95b5d44f4e752a1a4 to your computer and use it in GitHub Desktop.
[WordPress][Simple Map] Load Google Maps API script from Simple Map deferred.
<?php
defined( 'ABSPATH' ) or die( 'No direct access here.' );
/**
* Plugin Name: Simple Map | Defer
* Description: Makes sure Google Maps API script from <a href="https://wordpress.org/plugins/simple-map/">Simple Map</a> gets loaded deferred.
* Author: Caspar Hübinger
* Author URI: https://profiles.wordpress.org/glueckpress
* Plugin URI: https://gist.github.com/glueckpress/56c88900aad391b95b5d44f4e752a1a4
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Make defer.
* @link https://matthewhorne.me/defer-async-wordpress-scripts/
*/
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
// Defer
$defer_scripts = array( 'simplemap', 'google-maps-api' );
foreach( $defer_scripts as $defer_script ) {
if ( $defer_script === $handle ) {
return str_replace( ' src', ' defer="defer" src', $tag );
}
}
return $tag;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment