Skip to content

Instantly share code, notes, and snippets.

@dantetesta
Created December 20, 2023 16:42
Show Gist options
  • Save dantetesta/947e9a618ff0ef92c5462d6f92fe2ce1 to your computer and use it in GitHub Desktop.
Save dantetesta/947e9a618ff0ef92c5462d6f92fe2ce1 to your computer and use it in GitHub Desktop.
function get_spotify_iframe($atts) {
// Acesso global ao objeto $wpdb para operações de banco de dados
global $wpdb;
// Obtenção do ID da postagem a partir do atributo 'id'
$post_id = isset($atts['id']) ? intval($atts['id']) : 0;
// Se não houver um ID válido, retorna vazio
if (!$post_id) {
return '';
}
// Prefixo da tabela (geralmente definido no wp-config.php)
$table_prefix = $wpdb->prefix;
// Preparação da consulta SQL
$query = $wpdb->prepare("SELECT link_spotify FROM {$table_prefix}jet_cct_podcast WHERE _ID = %d", $post_id);
// Execução da consulta
$link_spotify = $wpdb->get_var($query);
// Verificação se um link foi encontrado
if (!$link_spotify) {
return ''; // Retorna vazio se não houver link
}
// Extrai o ID do episódio do Spotify da URL
preg_match('/episode\/([0-9A-Za-z]+)\?/', $link_spotify, $matches);
$spotify_id = $matches[1] ?? '';
if (!$spotify_id) {
return ''; // Retorna vazio se não conseguir extrair o ID
}
// Montagem do iframe com o ID do episódio do Spotify
$iframe = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/episode/' . $spotify_id . '?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>';
return $iframe;
}
add_shortcode('spotify_embed', 'get_spotify_iframe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment