Steps to create Shortcode for Google Ad Units
Shortcodes can be used for many purposes, using it for adsense is just one of them. In this article, we cover the creation of shortcode for Google Adsense Ads in Wordpress blog. Lets create a shortcode to display the adunit block of size 300 * 250. Step 1: Create a function to display Ad unit block of 300*250 under Child Themes's functions.php namely adsense_block(). [Note: Replace <client_id> and <ad_slot_id> with actual data].function adsense_block(){
return '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Text Ad -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="<client_id>"
data-ad-slot="<ad_slot_id>"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
Step 2: Link & Assign the newly created function as a new Shortcode namely "adsenseShortcode".
add_shortcode("adsenseShortcode", "adsense_block");
Step 3: Trying add this below line of code in one of the post or page of your website Refresh the page and you could see the ad unit 300 * 250 appearing in the location where you just inserted.About Shortcodes

Why do we need Shortcode for Adsense Blocks
- Flexibility to determine the specific ad unit positioning inside the post or page content
- By using shortcodes to Adsense units, scripts code is maintained in single file entry and can also achieve code Re-usability
- If we add adsense units in template files like header.php or sidebar.php, adunit placement would be made generic for all pages or post. But sometimes, we will need to display customized ad unit block for a specific post or page. This can be achieved by tagging a shortcode for the customized ad unit script code.