Last active
January 15, 2022 07:17
-
-
Save Digiover/9730bc39139c9c42a272a9b54368b3ff to your computer and use it in GitHub Desktop.
Create Google AdSense shortcode for in WordPress
This file contains 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
<?php | |
/* | |
* enqueue adsbygoogle.js in the footer & | |
* create a WordPress shortcode with your Google AdSense code | |
* follow me on Twitter: @Jan_Reilink | |
*/ | |
function saotn_loadAdsByGoogleJs() { | |
wp_register_script( 'google-adsense', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', '', '', true ); | |
wp_enqueue_script( 'google-adsense' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'saotn_loadAdsByGoogleJs' ); | |
function saotn_showAdSense() { | |
$output = '<ins class="adsbygoogle" | |
style="background-color: transparent; | |
text-decoration: none; | |
display:inline-block; | |
width:336px; | |
height:280px" | |
data-ad-client="ca-pub-xxxxxx" | |
data-ad-slot="xxxxxx"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>'; | |
return $output; | |
} | |
add_shortcode( 'showAdSense', 'saotn_showAdSense' ); | |
/* | |
* Usage: add [showAdSense] in your WordPress posts | |
* where you want your ad to appear | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A Dutch article with more information and usage:
https://www.itfaq.nl/google-adsense-wordpress-shortcode/