1

I have google adsense script which I can place it in different positions on my page.

I also have body text where the description of each post is and I want to know how can I add adsense script dynamically into my posts body text? (Google suggested to place it after second paragraph).

I am using laravel and this is how I get my body part of each post

{!! $post->body !!}

Sample of Google adsense code:

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

Any idea?

Update

single post function

//single post
public function single($slug)
{
        $post = Post::where('slug', $slug)->where('publish', '=', 'y')->firstOrFail();

        $post->addPageView();

        $previous = Post::where('slug', '<', $post->slug)->max('slug');

        $next = Post::where('slug', '>', $post->slug)->min('slug');

        $products = Product::all()->where('status', 'enable')->random(3);

        $categories = PostCategory::all();

        $settings = Setting::all();

        $author = AuthorInfo::where('user_id', $post->user->id)->first();

        return view('front.singlepost', compact('post', 'previous', 'next', 'products','categories', 'settings','author'));
}
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • Can you update with the controller used for the `post` page? – thisiskelvin Oct 23 '18 at 11:39
  • Is `$post->body` text preformatted with html tags like paragraph tags? You could insert the adsense text into the `body` by finding the 2nd

    [and using the `substr_replace` function.](https://stackoverflow.com/a/17880260/3585500)

    – ourmandave Oct 23 '18 at 11:40
  • @ourmandave yes it will print content of post body like `

    ....

    ` and whatever else we put while creating that content in ckeditor.
    – mafortis Oct 23 '18 at 11:49
  • @thisiskelvin i just get posts base on their `slug` and then in blade getting different column of each post like `$post->body` nothing special about controller part, however here it is: `$post = Post::where('slug', $slug)->where('publish', '=', 'y')->firstOrFail();` – mafortis Oct 23 '18 at 11:52
  • @ourmandave sorry but i didn't get how to use that `substr_replace` PS: I included my controller part in previous comment if it helps. – mafortis Oct 23 '18 at 11:54
  • @mafortis In what part of your code will be you adding your dynamic adsense code? – thisiskelvin Oct 23 '18 at 11:54
  • @thisiskelvin currently I just placed javascript code provided by google in blade (nothing in back-end) – mafortis Oct 23 '18 at 11:55
  • @mafortis So is each google adsense script unique to the post? – thisiskelvin Oct 23 '18 at 11:58
  • Also see [this answer](https://stackoverflow.com/a/36276123/3585500) on how to put adsense in blog posts. – ourmandave Oct 23 '18 at 11:58
  • @thisiskelvin no script is the same as my sample code in question i just want to place it after second paragraph of my `$post->body` – mafortis Oct 23 '18 at 12:02
  • @mafortis You mean after the first `

    ` tag?

    – thisiskelvin Oct 23 '18 at 13:05
  • @thisiskelvin after second one but even after first is ok – mafortis Oct 23 '18 at 13:44

1 Answers1

2

I haven't had a chance to test this out, however you can create an accessor (in this case getBodyWithAdsenseAttribute) which will create an altered version of the body content and include the adsense content after the 2nd paragraph:

Within your Post model file:

public function getBodyWithAdsenseAttribute()
{
    $javascript = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
      style="display:block"
      data-ad-client="ca-pub-6565454545454774"
      data-ad-slot="548855465655"
      data-ad-format="auto"
      data-full-width-responsive="true"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>';

    $paragraphs  = explode('</p>', $this->body); // Explode current body field
    $new_content = ''; // Variable for new content
    $count       = 1; // Set count for adding new content

    foreach ($paragraphs as $paragraph) {
        $new_content .= $paragraph;

        if ($count == 2) {
            $new_content .= $javascript;
        }

        $count++;
    }

    return $new_content;
}

Here, we store all the adsense data within a $javascript variable.

We then explode() the body content by the closing </p> tag, creating an array from the content.

Using a foreach(), we recreate the body content, counting to see if it is after the 2nd instance of the </p> tag. If so, we add the $javascript content to the new content.

Finally, we return all the content. This can be used within blade like the following

{!! $post->bodyWithAdsense !!}

Note: More code will be needed to fallback in case there is only one paragraph, or if there is no body content at all.

thisiskelvin
  • 4,136
  • 1
  • 10
  • 17