If you’re looking to integrate Spotify episode data into WordPress and store it in Advanced Custom Fields (ACF), this guide will walk you through the process. We’ll use the Spotify API to fetch episode details and automatically save them to ACF when a post is created.
Step 1: Get a Spotify API Access Token
To access the Spotify API, you need an access token. The following function retrieves the token using Spotify’s client_credentials authentication method.
function get_spotify_access_token() {
$client_id = '';
$client_secret = '';
if (!$client_id || !$client_secret) {
error_log('Spotify Client ID or Secret is missing');
return null;
}
$response = wp_remote_post('https://accounts.spotify.com/api/token', [
'body' => [
'grant_type' => 'client_credentials'
],
'headers' => [
'Authorization' => 'Basic ' . base64_encode("$client_id:$client_secret"),
'Content-Type' => 'application/x-www-form-urlencoded'
]
]);
if (is_wp_error($response)) {
error_log('Spotify API Token Request Failed: ' . $response->get_error_message());
return null;
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (!isset($body['access_token'])) {
error_log('Spotify API Token Missing: ' . print_r($body, true));
return null;
}
return $body['access_token'];
}Step 2: Fetch Episode Data from Spotify
Once we have the access token, we can use it to fetch episode data using the episode ID.
Step 3: Save Episode Data to ACF on Post Creation
We’ll hook into WordPress’ save_post action to fetch Spotify episode data whenever a post is created.
Step 4: Save Image and Set it as Featured Image
function download_and_attach_image($image_url, $post_id) {
// Get the upload directory
$upload_dir = wp_upload_dir();
$image_data = wp_remote_get($image_url);
if (is_wp_error($image_data)) {
error_log('Error fetching Spotify image: ' . $image_data->get_error_message());
return false;
}
$image_body = wp_remote_retrieve_body($image_data);
if (empty($image_body)) {
error_log('Spotify image response is empty.');
return false;
}
// Detect MIME type since URL lacks an extension
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_buffer($finfo, $image_body);
finfo_close($finfo);
error_log('Detected MIME type: ' . $mime_type);
$ext = '';
switch ($mime_type) {
case 'image/jpeg':
$ext = 'jpg';
break;
case 'image/png':
$ext = 'png';
break;
case 'image/gif':
$ext = 'gif';
break;
default:
error_log('Unsupported image type: ' . $mime_type);
return false;
}
// Generate unique filename
$filename = 'spotify-image-' . uniqid() . '.' . $ext;
$file_path = $upload_dir['path'] . '/' . $filename;
// Save the file
if (!file_put_contents($file_path, $image_body)) {
error_log('Failed to save Spotify image.');
return false;
}
error_log('Image saved successfully: ' . $file_path);
// Prepare file array for WordPress media upload
$file = [
'name' => $filename,
'type' => $mime_type,
'tmp_name' => $file_path,
'size' => filesize($file_path)
];
// Upload to media library
$attachment_id = media_handle_sideload($file, $post_id);
if (is_wp_error($attachment_id)) {
error_log('Failed to attach image: ' . $attachment_id->get_error_message());
return false;
}
error_log('Image uploaded successfully: ' . $attachment_id);
return $attachment_id;
}

2 comments
neoxphuse
I’m having issues with the featured image. Nothing is happening.
I’m trying to have this image thumbnail saved as the featured image here. Pasted the code along side the first 3, but no go.
https://open.spotify.com/episode/6bXZHhGH0hm3y6CIPAv05e?si=VUdMVydBSDaQ3WJXdA_c4w
Ivan Nugraha
Hi, i tried the url of podcast above and its working fine on my wordpress installation. It might be more easier if you have some dev site/staging which i could take a look into. You can contact me via email if you would like to.