Home
 

How to Place Google Ads or Any Other Code in BuddyPress Activity Stream Pages

I recently added the BuddyPress plugin to my daughter's site (diaryforteens.com). I'm constantly impressed with all of the extraordinary plugins and add-ons for WordPress. Shortly after adding BuddyPress to her site, people started joining and using this super cool feature. Naturally the AdSense wheels started turning. It became evident that there was no plugin to inject ads to the BuddyPress activity stream, and so here we are.
Check out DiaryForTeens.com Today!
Diary For Teens - Free Teen Blog Site

Ok, so let me start by saying I’m not a WordPress guru, not do a write plugins and I caution those of you who don’t want to venture into unknown territory. Secondly, this is probably something that would need to be fixed after applying any BuddyPress update as the file could be overwritten with a newer version. Ok if you’re still with me and want to place ads in your BuddyPress activity streams then have a look at the simple directions below.



Use your favorite FTP program or simply access your hosting directory by using the built in FTP functionality of windows folder explorer (yes you can FTP right out of your My Computer). Navigate to this folder within your WordPress installation > wp-content > plugins > buddypress > bp-themes > bp-default > activity. Download the entry.php file and make a copy of it somewhere. Open the original with your favorite text editor and search for a line that looks like this.
<?php bp_activity_content_body() ?>
Right below this line we’re going start counting how many post are being displayed on the page. We’re going to assign this to a variable. You’re going to insert this line.
<?php $adcnt=$adcnt+1;?>
Easy stuff here right? Ok now we’re going to insert the ad code. I’m only placing two ads in the stream as I already have an ad on my sidebar. In this example I’m inserting the ads on the second post and the fourth post. Change these numbers to whatever you want.
<?php if ($adcnt=="2" or $adcnt=="4") echo '**PASTE YOUR ADSENSE CODE HERE**';?>
And that’s it, the quick and dirty way to inject a couple of ads into your activity streams on BuddyPress pages. The ads will show up in the site wide activity stream as well as on profile pages which are basically activity streams for specific users. Post your questions if you get stuck. WordPress gurus… I know this is not the “proper” way to do it, but that’s how I roll. Here is the shamelees plug to help increase my daughter’s revenue… check out DiaryForTeens.com today!!!

15 Comments

  1. Mark:

    Update: a few updates ago, I started adding this into the buddypress/bp-themes/bp-default/activity/activity-loop.php

    I have this on line 34 of the activity-loop.php file in BuddyPress version 1.6 I’m using the BuddyPress ad manager to place ads elseware automatically. this code simply places an ad on after the first activity in an activity stream.

    $ad=$ad+1;

    if ($ad=="2") echo ' Your Adsense Code Here> ';

  2. Braham:

    This works great, thank you. But ads are appended after clicking the “Load More” button, so it ends up breaking Google’s 3-ad per page rule. How would this be changed to add it into the position, but only once and not again after that?

  3. Mark:

    Hi there, you must have a problem with your loop. Read my comment. I’ve had to adjust it a couple of times. If you’re still interested let me know and I can send you the cod I’m currently using. I’m using it in this site. http://www.diaryforteens.com/

  4. paul:

    Hi Mark,

    I have been looking for a plugin that does exactly the trick you posted here!

    I will test this snippet with the latest version of BuddyPress 1.9, but I would like to take a look at the code you’re using on your end (as it will be a great starting point for me, knowing that it already works elsewhere).

    Thanks for your time.

  5. Mark:

    Hi Paul, I’ve got to address my current version of BP and will likely have to replace the code. I’ll try to get to this soon and post the current code. It definitely makes a difference having those ads in page streams.

  6. paul:

    OK. I will check back to see.

  7. Jon:

    I’ve tried doing this in both entry.php and the activity loop but I am not having any success. Has anyone made any progress with the latest version of BP 2.0?

  8. Mark:

    I might try to update the site this weekend. I’ll reply to this post and let you know how I make out.

  9. Mark:

    Hi Jon, it still works. I’ve updated BP and changed the activity loop file. What is happeing when you try this? It’s probably something simple. I’ll try to make it easy. If you download this activity-loop.php file as stated in the post replace the first part of this while loop as shown below (I’ve only removed the closing PHP tag and pushed it down below my if statement).

    < ?php while ( bp_activities() ) : bp_the_activity(); $ad=$ad+1; if ($ad=="2") echo ' YOUR AD CODE '; ?>

  10. Mark:

    You could also just do it like this below the start of the while loop and leave that line in tact.
    < ?php $ad=$ad+1; if ($ad=="1" or $ad=="4" ) echo ' YOUR AD CODE HERE '; ?>

  11. Jon:

    Mark, Thanks for the reply. I did get it figured out and as you said, it was something simple. My theme was overriding the bp activity loop. Once I discovered that, everything worked fine. Thanks again for this. Looks and works great!

  12. Bheki Masinga:

    Hi All. I’m developing a community site using buddypress and i’m faced with the issue you guys are discussing. I have set up an AD server and i will also be using it on the live site. The code you are using is not working on my site. If i copy the Ad code from my server into the activity-loop.php file (below ), the banners do appear but not the way i want them. There’s an ad at the begining of the activity stream and two more after one post another one after two more posts. I want a way of controlling after how many updates the banners should appear.

  13. Ajay:

    Hi Jon, How did you get to know that your theme is overriding the activity loop, and can you also tell as to where can I find the same.

  14. Mel:

    Hi, doeIs there an update to this? I’m not able to find where to place the code.

  15. Rencontre adultère:

    I amm realloy glad to read this web site posts which consists of
    plenty of helpful facts, thanks for providing such information.

Leave a comment