Created
September 29, 2024 15:18
-
-
Save rhcarlosweb/fcc676ae88a5773e2cc13019d2edaa27 to your computer and use it in GitHub Desktop.
Shortcode WordPress to show Random Adsense
This file contains hidden or 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
// Adicione este código ao arquivo functions.php do seu tema ou em um plugin personalizado | |
function random_ad_shortcode($atts) { | |
static $ad_count = 0; | |
$ad_count++; | |
// Extrai os atributos do shortcode | |
$atts = shortcode_atts(array( | |
'ids' => '', // IDs das imagens, separados por vírgula | |
'link' => '', // URL do link do anúncio | |
'alt' => 'Anúncio', // Texto alternativo para a imagem | |
'event_category' => 'Anúncio', // Categoria do evento para o GA | |
'event_action' => 'Clique', // Ação do evento para o GA | |
'event_label' => 'Banner' // Rótulo do evento para o GA | |
), $atts); | |
// Divide os IDs das imagens em um array | |
$image_ids = array_map('trim', explode(',', $atts['ids'])); | |
// Prepara os dados das imagens | |
$images_data = array(); | |
foreach ($image_ids as $id) { | |
$image = wp_get_attachment_image_src($id, 'full'); | |
if ($image) { | |
$images_data[] = array( | |
'url' => $image[0], | |
'width' => $image[1], | |
'height' => $image[2] | |
); | |
} | |
} | |
// Gera um ID único para o anúncio | |
$ad_id = 'ad-' . $ad_count; | |
// Cria o HTML do anúncio | |
$output = sprintf( | |
'<div id="%1$s" class="random-ad" data-images="%2$s" data-link="%3$s" data-alt="%4$s" data-event-category="%5$s" data-event-action="%6$s" data-event-label="%7$s"></div>', | |
esc_attr($ad_id), | |
esc_attr(json_encode($images_data)), | |
esc_url($atts['link']), | |
esc_attr($atts['alt']), | |
esc_attr($atts['event_category']), | |
esc_attr($atts['event_action']), | |
esc_attr($atts['event_label']) | |
); | |
// Adiciona o script apenas uma vez por página | |
if ($ad_count === 1) { | |
add_action('wp_footer', 'random_ad_script'); | |
} | |
return $output; | |
} | |
add_shortcode('random_ad', 'random_ad_shortcode'); | |
function random_ad_script() { | |
?> | |
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
var ads = document.querySelectorAll('.random-ad'); | |
ads.forEach(function(ad) { | |
var images = JSON.parse(ad.dataset.images); | |
var randomImage = images[Math.floor(Math.random() * images.length)]; | |
var img = new Image(); | |
img.src = randomImage.url; | |
img.alt = ad.dataset.alt; | |
img.width = randomImage.width; | |
img.height = randomImage.height; | |
img.loading = 'lazy'; | |
var a = document.createElement('a'); | |
a.href = ad.dataset.link; | |
a.target = '_blank'; | |
a.rel = 'noopener noreferrer'; | |
a.onclick = function(e) { | |
if (typeof gtag === 'function') { | |
e.preventDefault(); | |
gtag('event', ad.dataset.eventAction, { | |
'event_category': ad.dataset.eventCategory, | |
'event_label': ad.dataset.eventLabel, | |
'transport_type': 'beacon', | |
'event_callback': function() { | |
window.open(ad.dataset.link, '_blank'); | |
} | |
}); | |
} | |
}; | |
a.appendChild(img); | |
ad.appendChild(a); | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment