XF 2.1 Render google ads from database

shqawe

Member
Hi,

I am building addon to render google ads from database the problem is if i insert a normal html img code i can easy render it but when i insert google ads the ads doesn't show and this is how i render the code:

Code:
$GoAdds = $this->getGoAdsQuery()->fetchOne();
return BbCode::renderFromHtml($GoAdds->ga_code);

but unfortunately it doesn't work so can someone help in this.
 
Why are you not using the Adsense code from Google? That's the only way you're going to get ads displayed. The AdSense bot needs to crawl your pages to determine which ads to show based on content/context. It won't do that without the proper code.
 
Hi @djbaxter, i'm already using Adsense code and this is not my problem my problem is how to render below code from my database into my posts

HTML:
<center>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XenArabiaHorizontal -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-000000000000000"
     data-adtest="on"
     google_adtest="on"
     data-ad-slot="7840750827"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
    </center>
 
I'm not sure what you're trying to achieve with your code, but it's currently attempting to convert your AdSense code into bb code. It's already html syntax, so if you want to display it, there's no need to process it any further.
 
@Lukas W.
What i'm actually want to do is to split post message to two parts and put Adsense ads between these parts and i did what you said but unfortunately its print code as html tags without show ads and below will find my php code for this processes

PHP:
public function getMsgForGa()
    {
        $fullMsg = explode("\n", $this->message);
        $GoAdds = $this->getGoAdsQuery()->fetchOne();
        $str = "";
        $i = 0;
        $advertise = \XF\Html\Renderer\BbCode::renderFromHtml($GoAdds->ga_code);
        
        foreach($fullMsg as $partMsg)
        {
            if($i == ceil(count($fullMsg)/2) ){
                
                    $str .= $partMsg."\n";
                    $str .= "\n";
                    $str .= $advertise;
                    $str .= "\n";

            }else{
                $str .= $partMsg."\n";
            }
            $i++;
        }
        

        return $str;
    }
 
Without knowing where this function is called in the stack I can't give you any advice. The BB Code renderer is escaping HTML, so you will need to insert your ad code after it is done rendering.

XF\Html\Renderer\BbCode::renderFromHtml renders code from html to bb code, so it's definitely not the right thing to use.
 
I called this function from post entity in post macro and i called it in template like this

HTML:
{{ bb_code($post.getMsgForGa(), 'post', $post) }}
 
That approach won't work. Any html inserted prior to the bb code rendering process is escaped. You'll have to pick another approach.
 
But this just happen just with google adsense code but when i try code like <img /> tag it's work perfectly and show any image i want so i think there is somwthing i miss or there is other function i should use:unsure:.
 
Your image tag works cause your approach renders it to the bb code equivalent which then can be rendered back to HTML.
 
Back
Top Bottom