2017
06.29

[SOLVED] Missing Gravatar Pictures

Today I noticed that the comment’s author pictures are broken. Some are visible, some are not.

Using Chrome’s Inspect Element function, I can see that the image URL starts with http://0.gravatar.com/avatar/. When I opened the image URL, Chrome shown “This site can’t be reached” error. My first instinct was replacing http with https. And voila… the the image loads.

Now I need to make this fix permanent on this blog. Digging through the sidebar source code, I found that the avatar image URL is produced by this function: get_avatar (codex URL). This function can be found inside file wp-includes\pluggable.php.

I proceed by adding this change at line 2415 (WordPress 4.8)

	if (strpos($url,"gravatar")>=0)
	{
		$url = str_replace("http://","https://",$url);
	}

Then I realized that my changes could be overwritten by WordPress future updates. Luckily, this function is pluggable. I can just copy over the whole function into my theme’s functions.php. I need to put the function into a plugin and then activate it. This is the only way to override the default get_avatar function.

Since I felt that my approach of using str_replace is probably the worst :D, I Googled for this topic. As it turned out, someone asked about this topic 3 years ago. In that page I confirm that my approach is indeed bad. There is a parameter to indicate what protocol Gravatar will use.

get_avatar( $id, $size, null, false, array('scheme' => 'https') );

By default this parameter is set to null. So I just need to change it to ‘https’.

function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
		$defaults = array(
			// get_avatar_data() args.
			'size'          => 96,
			'height'        => null,
			'width'         => null,
			'default'       => get_option( 'avatar_default', 'mystery' ),
			'force_default' => false,
			'rating'        => get_option( 'avatar_rating' ),
			'scheme'        => 'https', // 'scheme'        => null,
			'alt'           => '',
			'class'         => null,
			'force_display' => false,
			'extra_attr'    => '',
		);
   // ... SNIPPED ...
}

Finally, I bundled this function into a plugin called gravatar-secure. I didn’t bother to submit this plugin to WordPress Plugin Directory since it’s too simple πŸ˜€

So if you have similar issue to what I have, you can download the zip file below, extract and examine the source code. Once you’re sure my code is not malicious, you can upload the gravatar-secure folder into your WP’s plugins folder then activate the plugin.

Disclaimer: No guarantee, no warranty whatsoever. Do at your own risk.

the plugin in action here at this blog

2017
06.27

Still related to my last post, today we found another issue with Google Maps. This time, the issue is related to postal code resolution. As you can try yourself, we can enter a postal code in Google Maps, and we will see the correct location appear in the map.

After testing with a few test cases, it seems the result given by Google Maps API is not consistent. For example, examine these three URLs and compare their result:

URL A: Postal Code: 489949
URL B: Postal Code: 529684
URL C: Postal Code: 163009

Result A:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "489949",
               "short_name" : "489949",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore 489949",
         "geometry" : {
            "location" : {
               "lat" : 1.334575,
               "lng" : 103.949792
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.335923980291502,
                  "lng" : 103.9511409802915
               },
               "southwest" : {
                  "lat" : 1.333226019708498,
                  "lng" : 103.9484430197085
               }
            }
         },
         "place_id" : "ChIJh2cj7y892jERKFBGlnIii7Q",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

Result B:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "529684",
               "short_name" : "529684",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "51 Tampines Avenue 4, Singapore 529684",
         "geometry" : {
            "location" : {
               "lat" : 1.3527668,
               "lng" : 103.940085
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.354115780291502,
                  "lng" : 103.9414339802915
               },
               "southwest" : {
                  "lat" : 1.351417819708498,
                  "lng" : 103.9387360197085
               }
            }
         },
         "place_id" : "ChIJ2QytGRM92jERrMbjLH1pYVU",
         "types" : [ "establishment", "point_of_interest" ]
      }
   ],
   "status" : "OK"
}

Result C:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "9B",
               "short_name" : "9B",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Boon Tiong Road",
               "short_name" : "Boon Tiong Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "163009",
               "short_name" : "163009",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "9B Boon Tiong Road, Tiong Bahru View, Singapore 163009, 9B Boon Tiong Rd, Singapore 163009",
         "geometry" : {
            "location" : {
               "lat" : 1.2868142,
               "lng" : 103.8289788
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.288163180291502,
                  "lng" : 103.8303277802915
               },
               "southwest" : {
                  "lat" : 1.285465219708498,
                  "lng" : 103.8276298197085
               }
            }
         },
         "place_id" : "ChIJEZ_eH3kZ2jER_IcZMYi6gfQ",
         "types" : [ "establishment", "point_of_interest" ]
      }
   ],
   "status" : "OK"
}

So we can see here that we can’t rely on the postal code to get the address. But if we use the lat and long given in the result A, it will actually gives us the correct address πŸ˜€

URL A: Lat: 1.334575, Long: 103.949792:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "11",
               "short_name" : "11",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedok North Avenue 4",
               "short_name" : "Bedok North Ave 4",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "489949",
               "short_name" : "489949",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "11 Bedok North Ave 4, Singapore 489949",
         "geometry" : {
            "location" : {
               "lat" : 1.334575,
               "lng" : 103.949792
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.335923980291502,
                  "lng" : 103.9511409802915
               },
               "southwest" : {
                  "lat" : 1.333226019708498,
                  "lng" : 103.9484430197085
               }
            }
         },
         "place_id" : "ChIJb90H5S892jER41OGX1qDRV0",
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Opp Blk 3011",
               "short_name" : "Opp Blk 3011",
               "types" : [
                  "bus_station",
                  "establishment",
                  "point_of_interest",
                  "transit_station"
               ]
            },
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Opp Blk 3011, Singapore",
         "geometry" : {
            "location" : {
               "lat" : 1.3347473,
               "lng" : 103.9502402
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.336096280291502,
                  "lng" : 103.9515891802915
               },
               "southwest" : {
                  "lat" : 1.333398319708498,
                  "lng" : 103.9488912197085
               }
            }
         },
         "place_id" : "ChIJl27l8S892jERJRTu-a5zeOo",
         "types" : [
            "bus_station",
            "establishment",
            "point_of_interest",
            "transit_station"
         ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tampines, Singapore",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 1.375747,
                  "lng" : 103.985835
               },
               "southwest" : {
                  "lat" : 1.3144619,
                  "lng" : 103.924219
               }
            },
            "location" : {
               "lat" : 1.3495907,
               "lng" : 103.9567879
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.375747,
                  "lng" : 103.985835
               },
               "southwest" : {
                  "lat" : 1.3144619,
                  "lng" : 103.924219
               }
            }
         },
         "place_id" : "ChIJ0e4CNAQ92jERy_T_mqOSB48",
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 1.4708809,
                  "lng" : 104.0415799
               },
               "southwest" : {
                  "lat" : 1.216611,
                  "lng" : 103.6065099
               }
            },
            "location" : {
               "lat" : 1.3553794,
               "lng" : 103.8677444
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.470624,
                  "lng" : 104.0414138
               },
               "southwest" : {
                  "lat" : 1.2205143,
                  "lng" : 103.6097687
               }
            }
         },
         "place_id" : "ChIJyY4rtGcX2jERIKTarqz3AAQ",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "489949",
               "short_name" : "489949",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore 489949",
         "geometry" : {
            "location" : {
               "lat" : 1.334575,
               "lng" : 103.949792
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.335923980291502,
                  "lng" : 103.9511409802915
               },
               "southwest" : {
                  "lat" : 1.333226019708498,
                  "lng" : 103.9484430197085
               }
            }
         },
         "place_id" : "ChIJh2cj7y892jERKFBGlnIii7Q",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "489948",
               "short_name" : "489948",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Tampines",
               "short_name" : "Tampines",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore 489948",
         "geometry" : {
            "location" : {
               "lat" : 1.335123,
               "lng" : 103.949641
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.336471980291502,
                  "lng" : 103.9509899802915
               },
               "southwest" : {
                  "lat" : 1.333774019708498,
                  "lng" : 103.9482920197085
               }
            }
         },
         "place_id" : "ChIJz0_K_i892jERKuoi1XSScek",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 1.4784001,
                  "lng" : 104.0945001
               },
               "southwest" : {
                  "lat" : 1.1496,
                  "lng" : 103.594
               }
            },
            "location" : {
               "lat" : 1.352083,
               "lng" : 103.819836
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.4707592,
                  "lng" : 104.0884808
               },
               "southwest" : {
                  "lat" : 1.1587023,
                  "lng" : 103.6055575
               }
            }
         },
         "place_id" : "ChIJdZOLiiMR2jERxPWrUs9peIg",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

I guess we need to find another way to consistently able to resolve postal code. πŸ™

2017
06.20

My team have rolled out our Android application (more about this in future post) for the drivers to do the electronic POD (Proof of Delivery). It’s been two weeks of smooth sailing with minor issues, until today. Today we received complain from one of the Traffic Controller (the guy who assigns jobs to the driver) saying that the map showing the address incorrectly. The address, 11 Paya Lebar Road is shown in the middle of PIE.

In comparison, we tried to search on streetDirectory.com and WeGo.here.com

StreetDirectory.com: The “Right Here” vs the G (Google’s result)

wego.here.com

wego.here.com: The checkered circle vs Google’s Result

Well this is unexpected. I would expect that a tech giant like Google would point a location in Singapore correctly. After all, Singapore is the most connected country in the world. But in this case Google actually missed by … Hold on.. let me Google that for you …

It missed by 900 meters. I do hope Google will quickly look into this. We don’t want to rewrite our mobile apps using other mapping API. πŸ™

PS: If you are in Singapore, you will find this mistake on Uber or Grab (they are using Google Maps in their mobile Apps) by entering 11 Paya Lebar Road as your destination.