Am trying to change the album cover of an mp3 file during upload in wordpress. I facing the same error message “Unexpected response from the server. The file may have been uploaded successfully. Check in the Media Library or reload the page.”.
<?php
require_once 'getID3-master/getid3/getid3.php';
function update_mp3_metadata($upload)
{
$file_path = $upload['file'];
// Check if getID3 is loaded correctly
if ( !class_exists('getID3') ) {
echo 'getID3 library not found.';
return $upload;
}
$getID3 = new getID3;
$tagWriter = new getid3_writetags;
// Check if the uploaded file is an MP3
if ( mime_content_type($file_path) === 'audio/mpeg' ) {
$coverImagePath = get_template_directory() . '/public/images/yt.jpg';
// Check if the cover image path is correct and file exists
if ( !file_exists($coverImagePath) ) {
echo "Cover image not found at: $coverImagePath";
return $upload;
}
$tagData['attached_picture'][0] = [
'data' => file_get_contents($coverImagePath),
'picturetypeid' => 3,
'description' => 'Cover',
'mime' => 'image/jpeg',
];
$tagWriter->filename = $file_path;
$tagWriter->tagformats = ['id3v2.3'];
$tagWriter->overwrite_tags = true;
$tagWriter->tag_encoding = 'UTF-8';
$tagWriter->tag_data = $tagData;
if ( $tagWriter->WriteTags() ) {
echo 'Tags written successfully.';
} else {
echo "Couldn't write tags: " . implode(', ', $tagWriter->errors);
}
}
return $upload;
}
add_filter('wp_handle_upload', 'update_mp3_metadata');
?>
I started with the getId3 library included in WordPress in the “includes/ID3” directory, there’s no write.php file there. I thought that was the problem. Am using the downloaded library but still facing the same problem.
Businge Brian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.