简体   繁体   中英

How to convert a regular Google AdSense Ad in a responsive ad automatically using preg_match?

  • WordPress Theme options panel.
  • A textarea field where a user enter its non-responsive AdSense code.

I need that if a user enter a non-responsive adsense code, that code to be converted in a responsive code as follow:

NON REPONSIVE CODE:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- non responsive ads -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-12345"
     data-ad-slot="xxxxxxxxxxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

THE ABOVE CODE NEED TO BE CONVERTED IN:

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

I guess the job can be done using preg_match but how?

Thanks.

You can't do that (as far I know).

That (non-responsive) unit configuration is stored on Google servers:

{
  "status": "ACTIVE", 
  "kind": "adsense#adUnit", 
  "code": "xxxxxxxxxxxxxx", 
  "name": "non responsive ads", 
  "savedStyleId": "qwertzuiopšđžasdfghjklyxcvbnmqwerfvbgtzhnmjgbvfredcxsw", 
  "contentAdsSettings": {
    "type": "TEXT_IMAGE", 
    "backupOption": {
      "type": "BLANK"
    }, 
    "size": "SIZE_728_90"
  }, 
  "id": "ca-pub-12345:xxxxxxxxxxxxxx"
}

pagead2.googlesyndication.com will consider data-ad-slot="xxxxxxxxxxxxxx" ad requests as request for SIZE_728_90 format, and you can't change that "on the fly" - you need to create another ad unit if you need another size .

Even if you would do it and it looks like "it works", it is still a violation of AdSense policies, and it would pose a risk for all involved AdSense accounts and ad serving on your site. (Because "converted" code will make you some percent of invalid ad impressions, and / or bad requests.)

Modification of the AdSense ad code
https://support.google.com/adsense/answer/1354736?hl=en

Create a responsive ad unit
https://support.google.com/adsense/answer/3543893?hl=en

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM