So there’s a couple of things to correct.
Firstly, the sample code in the plugin’s documentation isn’t quite right. The second parameter in the call to add_filter
needs to be quoted, so that it looks like this …
add_filter('wp_quadratum_checkin', 'store_last_checkin', 10, 1);
Secondly, due to PHP’s rules on variable scoping, you need to declare references to the $last_checkin
variable as global otherwise PHP thinks you’re declaring another $last_checkin
variable with local scope to the function(s) it’s being referenced in.
Both of these are errors/omissions in the plugin’s documentation, which I’ll correct.
But thirdly, you can’t rely on the venue
‘s location
object always having a member called city
. If you look at the Foursquare API documentation for a checkin’s venue, it says that …
(location
is) an object containing none, some, or all of address
(street address), crossStreet
, city
, state
, postalCode
, country
, lat
, lng
, and distance
.
If the venue you last checked into doesn’t have a city
associated with it then your code will break as the city
member won’t be present. So you’ll need to check the location hierarchy and use the lowest granularity location that’s provided, falling back to the default is there’s no location metadata beyond the coordinates.
Also, the checkin object is just that, an object, so you’ll need to use object syntax, such as $last_checkin->venue->location
to reference the location
object and not array syntax, such as $last_checkin['venue']['location']
.
Putting all of this together, the following code should work (I’ve just written this and tested it out on my local WordPress install) …
<?php
$last_checkin = null;
add_filter('wp_quadratum_checkin', 'store_last_checkin', 10, 1);
function store_last_checkin($checkin) {
global $last_checkin;
$last_checkin = $checkin;
}
add_filter('wp_quadratum_strapline', 'format_strapline', 10, 2);
function format_strapline($content, $params) {
global $last_checkin;
$location = $last_checkin->venue->location;
$locality = null;
if (isset($location->city)) {
$locality = $location->city;
}
else if (isset($location->state)) {
$locality = $location->state;
}
else if (isset($location->country)) {
$locality = $location->country;
}
if ($locality !== null) {
return sprintf('<h5>Last seen at <a href="%s">%s</a> in %s',
$params['venue-url'],
$params['venue-name'],
$locality);
}
return $content;
}
?>
Hope this helps
-Gary