Reputation: 61
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.
Upvotes: 6
Views: 27073
Reputation: 9273
Why are you even creating a variable at all? It's easier to put this in a block of HTML:
// Exit PHP and return to HTML:
?>
<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>
<!-- return to PHP if necessary: -->
<?php
If you really need it to be in a variable, add ob_start();
at the beginning and $adsence = ob_get_clean();
at the end.
Upvotes: 0
Reputation: 1
<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>
Upvotes: 0
Reputation: 106
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>";
}
?>
Upvotes: 1
Reputation: 23379
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;
Upvotes: 3