How To Display Google Adsense Using WordPress Widgets The Right Way?

Let us learn how to install & display Google Adsense ads using WordPress widgets without repeating the Google Adsense script.

One of my friends has a moderately big WordPress news blog and the load speed was awefully slow. He had asked me to see if I can help with loading pages faster.

One of the things I found to speed up his blog was to reduce the number of HTTP requests and by deferring Javascripts.

Google Adsense ads is his primary source of income and I was shocked by the way, he used the Google Adsense code, in his blog. I told him that there is a better way to use Google Adsense code in WordPress and here I’m writing this blog post.

If you’re likely to follow along, I assume that you already have a Google adsense account and you’re familiar with WordPress widgets.

Let us say, you have the following code from Google Adsense dashboard.

The adsbygoogle.js is a common Javascript file that is required on all the pages where you want to display the ads. So this should be made to load only once on the web page.

Rather than using the <script> tag directly, we must use WordPress’s wp_enqueue_script function to include scripts on the page.

Now that we have our Google adsense javascript enqueued, we can go ahead and use the ad snippet on WordPress widgets.

If you’re using WordPress 4.8+, remember to paste the code in ‘Text mode’ under ‘Text’ widgets.

Using Text Widget to server Google Adsense Ads
Using Text Widget to server Google Adsense Ads
Using Text mode under Text Widgets
Google Adsense Ad being served
Google Adsense Ad being served

Tada! We have set up everything for our Adsense script to start serving ads. In addition to this, we could defer loading the script, so that we can avoid the ‘Render blocking Javascript’ issue. Google has developed an online tool – Google PageSpeed Insights that will help you identify other such issues in your website.

Learn more about the differences between defer and async attributes here.

Author: Maria Daniel Deepak

I'm a Web and WordPress developer who is exploring the minimalist lifestyle. Read more

Leave a Reply

Your email address will not be published. Required fields are marked *