RSS feed validity and Google Adsense

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!

Topic feedburner adsense feed Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.