How to display AdSense in WordPress tabs

Product:

WonderPlugin Tabs Plugin

Question:

I tried to add AdSense code to the tab, but it's not working. Can you please help?

My AdSense code is as follows:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="xxxxxxxxxx"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

Answer:

If you are adding the code to the first tab, that's, the tab is visible on page load, you can change the code to:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="xxxxxxxxxx"
     data-ad-format="auto"></ins>
<script>
jQuery(document).ready(function() {
     (adsbygoogle = window.adsbygoogle || []).push({});
});
</script>

The above code will initialise the AdSense when the document is ready.

If you are adding the code to the second or following tab, that's, the tab is invisible on page load, you can change the code to:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="xxxxxxxxxx"
     data-ad-format="auto"></ins>
<script>
var googleadinited = false;
var adtabid = 1;
jQuery(window).on("afterswitch.wonderplugintabs", function(event, id, prev_tab, current_tab){
  if (current_tab == adtabid && !googleadinited)
  {
    (adsbygoogle = window.adsbygoogle || []).push({});
    googleadinited = true;
  }
});
</script>

In the above code, there is a statement var adtabid = 1;. It works when you add the AdSense code to the second tab (the tab id starts from 0). If you add the AdSense to a different tab, make sure to change the number accordingly.

The code will initialise the AdSense when the second tab is clicked and switched on.