Skip to content

Instantly share code, notes, and snippets.

@Digiover
Last active January 15, 2022 07:17
Show Gist options
  • Save Digiover/9730bc39139c9c42a272a9b54368b3ff to your computer and use it in GitHub Desktop.
Save Digiover/9730bc39139c9c42a272a9b54368b3ff to your computer and use it in GitHub Desktop.
Create Google AdSense shortcode for in WordPress
<?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
*/
@Digiover
Copy link
Author

A Dutch article with more information and usage:
https://www.itfaq.nl/google-adsense-wordpress-shortcode/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment