-
AuthorPosts
-
October 26, 2021 at 7:21 am #1326442
Hi,
In my functions.php I use the code below since May 2020 to disable loading of Google Maps in both frontend and dashboard.
I would like to know if this code is still needed (even if set to disabled in theme options under Google Services) and if so, is this still the correct way to do it?Now in functions.php I use:
// Disable loading of Google Maps scripts in Enfold // otherwise keeps loading in Dashboard even when Google Maps is disabled in Enfold settings class av_google_maps { const API_URL = 'https://maps.googleapis.com/maps/api/js'; const API_VERSION = '3.38'; const MAPS_SEARCH_URL = 'https://www.google.com/maps/search/'; const AJAX_NONCE = 'av_google_maps_nonce'; static private $_instance = null; protected $key; protected $verified_key; protected $usage_count; protected $unconditionally_count; protected $maps_array; protected $loading_prohibited; static public function instance() { if( is_null( av_google_maps::$_instance ) ) { av_google_maps::$_instance = new av_google_maps(); } return av_google_maps::$_instance; } protected function __construct( $key = '' ) { $this->key = ''; $this->verified_key = ''; $this->usage_count = 0; $this->unconditionally_count = 0; $this->maps_array = array(); $this->loading_prohibited = null; } public function __destruct() {} public function handler_wp_register_scripts() {} public function handler_wp_enqueue_scripts() {} public function handler_wp_admin_enqueue_scripts() {} public function is_loading_prohibited() {} public function add_usage( $unconditionally = 'unconditionally' ) {} static public function api_url( $api_key = '', $callback = '' ) {} static public function api_destination_url( $latitude, $longitude ) {} static public function gmap_js_globals() {} public function handler_wp_footer() {} public function handler_wp_admin_footer() {} protected function check_api_key() {} public function get_key() {} protected function get_last_verified_key() {} protected function store_key( $key ) {} protected function delete_key() {} public function add_map( array $data, $add, $id = '' ) {} public function get_maps_count() {} public function backend_html( $api_key = '', $ajax = true, $valid_key = false ) {} } function Av_Google_Maps() { return av_google_maps::instance(); }
Thanks for your help and advice,
Rob
October 26, 2021 at 9:03 am #1326452Hey Rob,
Thank you for the inquiry.
You can now prevent scripts from loading when they are not required by setting the Enfold > Performance > Disabling of template builder elements settings to the second option. You can also manually enable/disable the elements including the Google Map Element by selecting the third option. This will show a list of all elements and their current status (in use or not).
Best regards,
IsmaelOctober 26, 2021 at 10:02 am #1326459Hi Ismael,
Thanks for the quick answer. I already had that second option set (load only used elements).
So, let me rephrase the question: is the code I have now in my functions.php obsolete when using this option?
And even more importantly, does it also disable the backend (admin area) loading of the Google Maps mess? :-)Regards,
RobP.S.
The reason I’m asking again is that with the code mentioned above commented out, but the option “only load used elements” set, I still see this in QueryMonitor in the admin:Footer:
https://maps.googleapis.com/maps/api/js?v=3.45
wp-content/themes/enfold/framework/js/conditional_load/avia_google_maps_api.js
wp-content/themes/enfold/framework/js/conditional_load/avia_google_recaptcha_front.jsSo it’s almost the same question for Google ReCaptcha, which I do not use at all. Why is it loading at all here?
Added info 2021-10-26 14:52
Even stranger, on an exact copy for DEV of that DEVsite, one has the maps script in the backend and the other doesn’t… ?
Also found this:July 22nd 2020 – Version 4.7.6
added: filter avf_skip_enqueue_scripts_backend_grecaptcha: supress loading of Google reCaptcha scripts in backendand this:
https://kriesi.at/support/topic/proper-method-to-disable-google-maps-in-enfold/
- This reply was modified 3 years ago by rob2701.
October 26, 2021 at 11:08 pm #1326547Hi,
To be sure of what I said, I have restored a fresh backup on the DEV website, commented out the Google Maps code in functions.php (lines 538-596) and installed Query Monitor so you can check for yourself. I have disabled compression in the theme for now.
Without that code Google Maps js is loaded in the admin area. Google ReCaptcha is also loaded in the admin area.
The options you mentioned earlier in the theme settings work fine, but only for the frontend. In the backend things are like I described here.Login info in private window. Thanks for taking a look at this,
Rob
October 27, 2021 at 5:11 am #1326588Hi,
Thank you for the clarification.
The scripts are required in the backend because the theme has to validate the API key, which is needed in order to use the map API. If you need to completely disable the scripts for some reason, you can use the wp_dequeue_script function.
// https://developer.wordpress.org/reference/functions/wp_dequeue_script/
You can find the script handles in the following code..
wp_register_script( 'avia_google_maps_front_script' , AVIA_JS_URL . 'conditional_load/avia_google_maps_front.js', array( 'jquery' ), $vn, true ); wp_register_script( 'avia_google_maps_api_script' , AVIA_JS_URL . 'conditional_load/avia_google_maps_api.js', array( 'jquery' ), $vn, true ); wp_register_script( 'avia_google_maps_widget_admin_script' , AVIA_JS_URL . 'conditional_load/avia_google_maps_widget_admin.js', array( 'jquery', 'avia_google_maps_api_script' ,'media-upload' ,'media-views' ), $vn, true );
,.. within the enfold/framework/php/class-gmaps.php file (line 137).
Best regards,
IsmaelOctober 27, 2021 at 10:27 am #1326635Hi Ismael,
Thanks for the info and time. Yes, it’s nice that I can manually take care of it.
Yes, I need to disable these scripts and the link to Google(!). The same for Google ReCaptcha script.
Not only are they not needed when not used, they also slow down the backend when Google can’t be reached for some reason.That is exactly my point: if I don’t use Google Maps at all, why would Enfold need to validate the API key at all? Same for ReCaptcha.
I think this is without doubt something that needs to be handled by the general theme settings as well, automatically. When elements are not used, then the associated scripts and styles should not load anywhere, neither frontend nor backend. They should ONLY load when required. Dot.
You say: the scripts are required in the backend because the theme has to validate the API key, which is needed in order to use the map API. I say NO. The theme doesn’t need to validate the API key, not until I start to choose to use the GMaps element…
And the same holds true for any other theme component, especially when external / third party..
Have a good day,
Rob
- This reply was modified 3 years ago by rob2701.
October 28, 2021 at 11:18 am #1326798Hi,
Thank you for the info.
We will forward the issue to our channel for further consideration. We might have to check first if the “Enable Google Maps on your site” is actually set to the second option before loading the required scripts.
Best regards,
IsmaelOctober 28, 2021 at 11:27 am #1326800Hi Ismael,
As usual thanks to you and the rest of the team for your attention and for all the hard work. Much appreciated! And thanks to all for taking a look at this.
On a related note, when testing several options,I found that these two filter generate warnings, you may also want to look at that. Neither seems to work for the backend, by the way.
// Google Maps solution after Enfold 4.5.x // -- this disables all googlemap scripts handles frontend: avia_google_maps_front_script // -- and backend: avia-google-maps-api, avia_google_maps_api_script add_filter( ‘avf_load_google_map_api_prohibited’, ‘__return_true’ ); // This disables loading of Google ReCaptcha in the backend add_filter( ‘avf_skip_enqueue_scripts_backend_grecaptcha’, ‘__return_true’ );
Warnings:
Warning Use of undefined constant ‘avf_load_google_map_api_prohibited’ - assumed '‘avf_load_google_map_api_prohibited’' (this will throw an Error in a future version of PHP) Warning Use of undefined constant ‘avf_skip_enqueue_scripts_backend_grecaptcha’ - assumed '‘avf_skip_enqueue_scripts_backend_grecaptcha’' (this will throw an Error in a future version of PHP)
Correction:
My bad! That’s what I get as punishment for not wrapping it in a function… Embarassing…Have a good day!
RobP.S.
This code from this post still seems to work however:
https://kriesi.at/support/topic/proper-method-to-disable-google-maps-in-enfold/#post-1186977- This reply was modified 3 years ago by rob2701. Reason: Added link to code in pastebin from a post from Nikko
October 28, 2021 at 2:07 pm #1326839Hi,
Thank you for the info.
Looks like using the filters that you mentioned above will help suppress the scripts. Try to add this code in the functions.php file.
add_filter("avf_skip_enqueue_scripts_backend_gmaps", function($skip) { return "skip_loading"; }, 10, 1); add_filter("avf_load_google_map_api_prohibited", function($prohibit) { return true; }, 10, 1);
The filters above will disable these actions.
add_action( 'wp_enqueue_scripts', array( $this, 'handler_wp_enqueue_scripts' ), 500 ); add_action( 'admin_enqueue_scripts', array( $this, 'handler_wp_admin_enqueue_scripts' ), 500 );
Best regards,
IsmaelOctober 28, 2021 at 2:09 pm #1326841i use it in this way to determine a conditional loading – but it might work with global setting too: Link
October 28, 2021 at 2:19 pm #1326844Hi Ismael and Guenni,
Thanks for the tips, great, I will certainly try them out. And I will also try to learn to be more careful and not so hasty when typing filter code :-)
I always get myself into trouble that way… embarassing.Thanks for helping out!
Rob
October 28, 2021 at 2:27 pm #1326847Hi,
No problem. Nothing embarrassing about that. We make mistakes, we learn. All is good. :)
Best regards,
IsmaelOctober 28, 2021 at 2:42 pm #1326850Thanks, you are right about the mistakes and the learning, but it is also about remembering what I learned once before :-)
I will let you know here after I try them out later tonight.October 28, 2021 at 5:51 pm #1326882Hi,
Just to let you know that your code for the filters to prevent the backend loading of Google Maps and Google Maps API works just fine. They are gone from the backend, thanks! I’m a happy camper. :-)
I added the one for dequeueing the Google ReCaptcha js as well, and that works fine too:// Disable loading of Google ReCaptcha script in theme backend add_filter("avf_skip_enqueue_scripts_backend_grecaptcha", function($skip) { return "skip_loading"; }, 10, 1);
I wish I had found these filters and the proper way to use them earlier, then I wouldn’t have had to waste so much of your time. :-)
Thanks again for your help and patience and have a good evening (or morning/afternoon/night depending on where you are).Solved.
Rob
October 30, 2021 at 6:15 pm #1327085Hi,
Glad Ismael could help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.Best regards,
Mike -
AuthorPosts
- The topic ‘Validity of code to disable Google Maps’ is closed to new replies.