6

How to echo AdSense code with PHP? Here is the sample of my code which I am working for a codeigniter php.

$adsence = " 

<div class=\"right-inner\">
            <center width=\"96% class=\"img-responsive center-block\">
                            <script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>

<!-- quiz_net -->
              <ins class=\"adsbygoogle\"
                 style=\"display:block\"
                 data-ad-client=<?php echo $client ?>
                 data-ad-slot=<?php echo $slot ?>
                 data-ad-format=\"auto\"></ins>
              <script>
              (adsbygoogle = window.adsbygoogle || []).push({});
              </script>
            </center>     
</div>";

echo $adsence;

All I want to insert adsense code inside a div with PHP. I also tried with htmlentities along with stripslashes but ad in not getting displayed.

1
  • I'm thinking the async property on your Adsense JS script is the issue i.e. the (adsbygoogle = window.adsbygoogle || []).push({}); block is run before the script is downloaded, initialized. Try removing it. Mar 1, 2016 at 0:01

3 Answers 3

3

data-ad-client=<?php echo $client ?>

you're already in the php parser, don't need to open it again

data-ad-client=$client

fix the other spot where u did that too


$adsence = " 
<div class=\"right-inner\">
            <center width=\"96% class=\"img-responsive center-block\">
                            <script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>

<!-- quiz_net -->
              <ins class=\"adsbygoogle\"
                 style=\"display:block\"
                 data-ad-client=\"$client\"
                 data-ad-slot=\"$slot\"
                 data-ad-format=\"auto\"></ins>
              <script>
              (adsbygoogle = window.adsbygoogle || []).push({});
              </script>
            </center>     
</div>";

echo $adsence;
0
1

I add my idea and I hope it will be useful to you.

You can use queries in the database if you have a managed site or you can use a configuration file to not always exploit the source code.

Like:

// config_file.php

$adsense = "0"; // 0 - For unactivate , 1 - For activate
$ads_client_id = "ca-pub-1234567890123456"; // Publisher ID
$ads_slot_id   = "123456789"; // Slot ID
$ads_format    = "auto"; // ADS format - auto (This ad unit can automatically adjust the size of space available on the page.)

// code embed

<?php if($adsense == 1){
echo "<script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>
<ins class=\"adsbygoogle\"
     style=\"display:block\"
     data-ad-client=\"{$ads_client_id}\"
     data-ad-slot=\"{$ads_slot_id}\"
     data-ad-format=\"{$ads_format}\"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>";
}
?>
0
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-1325790438113768",
          enable_page_level_ads: true
     });
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.