1

I want to use Google Adsense code snippets in my blog posts, so in my single.php. However, when I do so, then my RSS feed becomes invalid because of the javascript code in the content and the feed won't be updated anymore.

Is there ANY way to fix this?

I still have the Google Adsense code in the sidebar where it's not causing any issues.

I've seen lots of blogs that have Google ads in their blog posts and a functioning RSS feed, so it seems to be possible, but how? Or rather, why is it causing problems in my case?

I've done a looooooooot of research, but couldn't find ANY information about this issue. I think that means that it's not a very common problem. I really hope somebody can help me with this BIG problem.

Would something like this work? http://chaosinacan.com/programming/adsense-without-blocking/

For reference, here's my website: http://zoomingjapan.com/

Here's my feed: http://zoomingjapan.com/feed/

Thanks a lot in advance!

7
  • How do you add the adsense code to the blog posts?
    – fuxia
    Commented Nov 10, 2012 at 4:00
  • @toscho Well, because I didn't want to lose the validity of my feed, I took all the codes out again, but I did put every single code in directly into each post as I was still experimenting a lot. Later on I wanted to place a code snippet into the single.php instead, but that would be essentially the same thing, right?
    – japanworm
    Commented Nov 10, 2012 at 16:28
  • @toscho I assume your question means that there are different ways to implement the code to your blog posts. Could you enlighten me, please?
    – japanworm
    Commented Nov 12, 2012 at 4:27
  • Well, I guess, it is a filter for the_content, but I really have to see that to give answer.
    – fuxia
    Commented Nov 12, 2012 at 4:39
  • @toscho Sure! Let me know what exactly you need to see and I'll provide the code :)
    – japanworm
    Commented Nov 12, 2012 at 9:12

1 Answer 1

1

Do not paste the Adsense code into the blog posts. Use a plugin instead.

Sample Plugin:

<?php
/* Plugin Name: Zoomingjapan Adsense */

add_filter( 'the_content', 'zoomingjapan_adsense' );

function zoomingjapan_adsense( $content )
{
    if ( is_feed() or ! is_singular() )
        return $content;

    $adsense = '<adense code, replace that!>';

    // remove all existing script elements
    $content = preg_replace("~<script[^>]*>.+</script[^>]*>~i", '', $content );

    return $content . $adsense;
}

Usage:

  • Change the content of the variable $adsense to whatever you need.
  • Put the file into wp-content/plugins/.
  • Activate it in wp-admin/plugins.php.

That’s all.

20
  • 1
    See this answer for the where (plugin, not theme!). And, please, ignore that terrible tutorial. They are doing it wrong. It hurts just to look at that, ah, it huuuurtssss …
    – fuxia
    Commented Nov 12, 2012 at 15:59
  • 1
    Remove the BOM
    – fuxia
    Commented Dec 9, 2012 at 14:22
  • 2
    This function is a filter you have to pass some content to it. The plugin does that automatically.
    – fuxia
    Commented Dec 9, 2012 at 17:23
  • 1
    Sorry, this thread crossed the too localized line long ago, and I really don’t have the time for further support or a probably long chat session.
    – fuxia
    Commented Dec 10, 2012 at 8:48
  • 1
    That's okay. I'll mark your answer as accepted for now as it did help. It's not your fault that I'm too much of a beginner to know how to solve things by myself. Thanks.
    – japanworm
    Commented Dec 10, 2012 at 9:03

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.