Last active
December 10, 2015 21:58
-
-
Save drainpip/4498231 to your computer and use it in GitHub Desktop.
Raw PHP for Wordpress / Songkick integration. Further work is needed on making this into a more robust function using Wordpress' current features.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Artist is custom field key for post | |
$key_songkick = "Artist"; | |
// Check post to see if Artist custom field exists | |
if ( get_post_meta($post->ID, $key_songkick, true) ) : | |
// Need to search by Artist name since we don't know ID, need ID to get the artist calendar. | |
// Using urlencode since we can't send a space to the Songkick JSON request | |
$Artist = urlencode(get_post_meta($post->ID, $key_songkick, true)); | |
// Use your API key here | |
$jsonurl = "http://api.songkick.com/api/3.0/search/artists.json?query=$Artist&apikey=[YourSongKickAPIKey]"; | |
// PHP decoding JSON output | |
$json = file_get_contents($jsonurl,0,null,null); | |
$json_output = json_decode($json); | |
// Setting variables for next query, mainly we needed the ID, but I'm using the URI and Artist name below in case we can't find any upcoming shows | |
$URL = $json_output->resultsPage->results->artist[0]->uri; | |
$ArtistID = $json_output->resultsPage->results->artist[0]->id; | |
$Artist_Text = get_post_meta($post->ID, $key_songkick, true); | |
// Using the ID we pulled from name search and decoding that JSON request | |
$jsonurlCalendar = "http://api.songkick.com/api/3.0/artists/$ArtistID/calendar.json?apikey=[YourSongKickAPIKey]"; | |
$jsonCalendar = file_get_contents($jsonurlCalendar,0,null,null); | |
$json_outputCalendar = json_decode($jsonCalendar); | |
// if statement to make sure there are upcoming events | |
if ($json_outputCalendar->resultsPage->totalEntries > 0): | |
// Building array from JSON request, this is to pull the first three items of course, but you can use dynamic array to make as many as you want. | |
$ArtistCalendar = array ( | |
"EventName" => array ( | |
$json_outputCalendar->resultsPage->results->event[0]->displayName, | |
$json_outputCalendar->resultsPage->results->event[1]->displayName, | |
$json_outputCalendar->resultsPage->results->event[2]->displayName | |
), | |
"EventCity" => array ( | |
$json_outputCalendar->resultsPage->results->event[0]->location->city, | |
$json_outputCalendar->resultsPage->results->event[1]->location->city, | |
$json_outputCalendar->resultsPage->results->event[2]->location->city | |
), | |
"EventURL" => array ( | |
$json_outputCalendar->resultsPage->results->event[0]->uri, | |
$json_outputCalendar->resultsPage->results->event[1]->uri, | |
$json_outputCalendar->resultsPage->results->event[2]->uri | |
) | |
); | |
// Outputs full multidimensional array into site style | |
foreach ($ArtistCalendar["EventName"] as $Key => $Name) | |
{ | |
echo "<p><a href='{$ArtistCalendar["EventURL"][$Key]}' target='_blank'>$Name</a><br><span class='City'>{$ArtistCalendar["EventCity"][$Key]}</span></p>"; | |
} | |
endif; | |
// This will always display, but if there are no upcoming events, it will at least show this link so a user can follow a band on Songkick directly | |
echo "<p>See upcoming shows for <a href='$URL' target='_blank'>$Artist_Text</a></p>"; | |
else: | |
// If the Artist custom field was not set for the post | |
echo "<p>Check out <a href='http://www.songkick.com/' target='_blank'>Songkick</a> to keep track of your favorite bands touring now.</p>"; | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment