'How to insert Google Adsense Code in PHP script?
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.
Solution 1:[1]
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;
Solution 2:[2]
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>";
}
?>
Solution 3:[3]
<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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | I wrestled a bear once. |
Solution 2 | AdyDev |
Solution 3 | Stephen |