Support » Plugin: Maps Builder - Google Maps Plugin » Invalid Argument

  • Resolved Demonslay335

    (@demonslay335)


    Hi,

    My website started outputting this error at the top of every page for users who are not logged in:

    Warning: Invalid argument supplied for foreach() in /home/{site}/public_html/wordpress/wp-content/plugins/google-maps-builder/public/class-wordpress-google-maps.php on line 537

    WordPress v4.1, Google Maps Builder v1.0.3.

    The erroneous line is the following (function check_for_multiple_google_maps_api_calls()):

    //loop through registered scripts
    foreach ( $wp_scripts->registered as $registered_script ) {
    	// Loop code...
    }

    When logged in (as an administrator, not sure if that part matters), the error does not occur. If I do a var_dump($wp_scripts) just before the loop, it is NULL for a guest.

    I propose the following patch to prevent the error, inserted just before the loop in question. I haven’t dug further to see why the global is NULL in the guest state, but this works as defensive programming anyways.

    if(is_null($wp_scripts) || is_null($wp_scripts->registered)){
    
    	return $multiple_google_maps_api;
    
    }

    Thank you for your consideration.

    https://wordpress.org/plugins/google-maps-builder/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Devin Walker

    (@dlocc)

    Hey Demonslay,

    We caught that too! Thanks for your help. Release coming soon with the fix.

    Awesome, thanks for the quick reply. I look forward for that release. 🙂

    I got that error too. Any idea when that fix will be released? Holding up my launch at the moment.

    Plugin Author Devin Walker

    (@dlocc)

    Hey Nimeck – can you reference the fix above?

    I tried, but I don’t think I did it write. Here is what I entered:

    //loop through registered scripts
    		foreach ( $wp_scripts->registered as $registered_script ) {
    
    			if(is_null($wp_scripts) || is_null($wp_scripts->registered)){
    
    				return $multiple_google_maps_api;
    
    			}
    
    			//find any that have the google script as the source, ensure it's not enqueud by this plugin
    			if ( strpos( $registered_script->src, 'maps.googleapis.com/maps/api/js' ) !== false && strpos( $registered_script->handle, 'google-maps-builder' ) === false ) {
    
    				$multiple_google_maps_api = true;
    				//ensure we can detect scripts on the frontend from backend; we'll use an option to do this
    				if ( ! is_admin() ) {
    					update_option( 'gmb_google_maps_conflict', true );
    				}
    
    			}
    
    		}
    Plugin Author Devin Walker

    (@dlocc)

    Yeah that won’t work. Can you email us at support(at) wordimpress.com and we’ll send you the updated file?

    nimeck,

    My proposed patch would look as follows:

    if(is_null($wp_scripts) || is_null($wp_scripts->registered)){
    
    	return $multiple_google_maps_api;
    
    }
    
    //loop through registered scripts
    foreach ( $wp_scripts->registered as $registered_script ) {
    	// Loop code...
    }

    Note that the code I suggested goes before the loop, to prevent the loop from crashing if the variable is NULL unexpectedly.

    Thanks guys! I’ve got it working now.

    I got the same error….
    where exactly i have to pot that code ?….
    in what file .php ? function.php ???
    please help

    Warning: Invalid argument supplied for foreach() in /home/graziano1975/public_html/viveremaiorca.it/wp-content/plugins/google-maps-builder/public/class-wordpress-google-maps.php on line 537

    grazianochiappa,

    The error actually tells you exactly which file the error occurs in. You can either use FTP and edit the file remotely, or use the WordPress admin panel to edit the file.

    The file in question is class-wordpress-google-maps.php, the full path is in the warning itself.

    Done, it works

    http://www.viveremaiorca.it

    now please
    like and share ! :-))

    thank you

    Demonslay335

    Thank you so much. I do not know much English so I apologize for spelling errors.

    I am a beginner using wordpress and my knowledge of English is not enough, however, intuitively followed your advice and it works !!

    Thank you so much again.

    I have made the update to this file class-wordpress-google-maps.php in the path given by the warning msg. See http://www.nrfhideout.com. Message text is “Warning: Invalid argument supplied for foreach() in /home1/ambruck/public_html/wp-content/plugins/google-maps-builder/public/class-wordpress-google-maps.php on line 537”. Doesn’t fix the problem. Function is copied in here:

    public function check_for_multiple_google_maps_api_calls() {

    global $wp_scripts;

    $multiple_google_maps_api = false;

    if(is_null($wp_scripts) || is_null($wp_scripts->registered)){

    return $multiple_google_maps_api;

    }

    //loop through registered scripts
    foreach ( $wp_scripts->registered as $registered_script ) {

    //find any that have the google script as the source, ensure it’s not enqueud by this plugin
    if ( strpos( $registered_script->src, ‘maps.googleapis.com/maps/api/js’ ) !== false && strpos( $registered_script->handle, ‘google-maps-builder’ ) === false ) {

    $multiple_google_maps_api = true;
    //ensure we can detect scripts on the frontend from backend; we’ll use an option to do this
    if ( ! is_admin() ) {
    update_option( ‘gmb_google_maps_conflict’, true );
    }

    }

    }

    //Ensure that if user resolved conflict on frontend we remove the option flag
    if ( $multiple_google_maps_api === false && ! is_admin() ) {
    update_option( ‘gmb_google_maps_conflict’, false );
    }

    //returns boolean
    return $multiple_google_maps_api;
    }

    ambruck,

    Please be sure that you have edited the correct file. It looks like, from the line number, it may have not taken your change properly. The line number for the error stayed the same (537) even with the addition of code, which should have pushed the afflicted line down.

    Here is what the whole function should look like.

    /**
     * Load Google Maps API
     *
     * @description: Determine if Google Maps API script has already been loaded
     * @since      : 1.0.3
     * @return bool $multiple_google_maps_api
     */
    public function check_for_multiple_google_maps_api_calls() {
    
    	global $wp_scripts;
    
    	$multiple_google_maps_api = false;
    
    	/**
    	*	Prevent crashes for guests
    	*	@link	https://wordpress.org/support/topic/invalid-argument-25
    	*/
    	if(is_null($wp_scripts) || is_null($wp_scripts->registered)){
    
    		return $multiple_google_maps_api;
    
    	}
    
    	//loop through registered scripts
    	foreach ( $wp_scripts->registered as $registered_script ) {
    
    		//find any that have the google script as the source, ensure it's not enqueud by this plugin
    		if ( strpos( $registered_script->src, 'maps.googleapis.com/maps/api/js' ) !== false && strpos( $registered_script->handle, 'google-maps-builder' ) === false ) {
    
    			$multiple_google_maps_api = true;
    			//ensure we can detect scripts on the frontend from backend; we'll use an option to do this
    			if ( ! is_admin() ) {
    				update_option( 'gmb_google_maps_conflict', true );
    			}
    
    		}
    
    	}
    
    	//Ensure that if user resolved conflict on frontend we remove the option flag
    	if ( $multiple_google_maps_api === false && ! is_admin() ) {
    		update_option( 'gmb_google_maps_conflict', false );
    	}
    
    	//returns boolean
    	return $multiple_google_maps_api;
    }

    If you continue to have issues, I can send you the file.

    Thanks so much, the old pages must still have been cached (I’m using WP Super Cache), because today the message has disappeared!
    All seems to work now 🙂

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Invalid Argument’ is closed to new replies.