Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
How do you add the adsense code to the blog posts? –  toscho Nov 10 '12 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 Nov 10 '12 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 Nov 12 '12 at 4:27
    
Well, I guess, it is a filter for the_content, but I really have to see that to give answer. –  toscho Nov 12 '12 at 4:39
    
@toscho Sure! Let me know what exactly you need to see and I'll provide the code :) –  japanworm Nov 12 '12 at 9:12

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
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 … –  toscho Nov 12 '12 at 15:59
1  
Remove the BOM –  toscho Dec 9 '12 at 14:22
2  
This function is a filter you have to pass some content to it. The plugin does that automatically. –  toscho Dec 9 '12 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. –  toscho Dec 10 '12 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 Dec 10 '12 at 9:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.