Untitled - PHP 0.99 KB
                                
                                    $videoId = $matches[2][0];
    $youtubeImageSrc = "https://img.youtube.com/vi/$videoId/";

    //  
    $possibleThumbnails = ['maxresdefault.jpg', 'hqdefault.jpg', 'mqdefault.jpg'];
    $youtubeImage = '';

    foreach ($possibleThumbnails as $thumbnail) {
        $headers = get_headers($youtubeImageSrc . $thumbnail, 1);
        if ($headers && strpos($headers[0], '200 OK') !== false) {
            $youtubeImage = $thumbnail;
            break;
        }
    }

    if (!$youtubeImage) {
        error_log('No valid YouTube thumbnail found.');
        return;
    }

    $thumbnailUrl = $youtubeImageSrc . $youtubeImage;

    //
    $response = wp_safe_remote_get($thumbnailUrl);
    if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
        error_log('Error fetching the thumbnail: ' . $response->get_error_message());
        return;
    }

    $imageData = wp_remote_retrieve_body($response);
                                
                            

Paste Hosted With By Wklejamy.pl