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

      [adsenseShortcode]

Refresh the page and you could see the ad unit 300 * 250 appearing in the location where you just inserted.

About Shortcodes

Shortcodes for Google Ad Units In WordPress
Shortcodes for Google Ad Units In WordPress

Shortcodes can be called as User defined functions in wordpress. We can create required scenario under a function name and tag it as a shortcode. When we invoke this tagged function with a shortcode name, its underlying function would be invoked. Shortcodes can be added in the template PHP files or directly in the post or page.

Ability to add it directly in the post or page is the key advantage of shortcodes. It helps to access the required functionality(without touching the code) just by typing the shortcode name very much like adding a paragraph to the post or page.

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.

Usage of Plugins’

We have to be very selective when it comes to the usage of Plugins in wordpress.  Totally agree that plugins comes in handy for almost all the scenarios that we encounter during the development days with WordPress blogs. But, in the due course of time, we tie too many dependencies of the website to the plugins and it is a big hassle to cleanup. Also, it considerably adds extra load to the server often causing the slowness in the website.

For small functionalities’ like adding social media icons, Popular posts’ display, creation of child theme, etc., we can do away without the plugins’ usage. So, before we start the hunt for plugins to achieve a functionality, lets give a thought process and then decide to go with plugins or not.

Leave a Reply