BuddyDev

Search

how to make this code work properly to put ads in buddypress activity

  • Participant
    Level: Initiated
    Posts: 5
    lucas on #20787

    i use this code my modemlooper `function custom_markup_before_every_fifth_activity() {
    global $activities_template;

    $item_5 = $activities_template->activities[4];

    if ( $item_5->id === $activities_template->activity->id ) {

    echo ‘adsense code here’;
    }}
    };
    add_action( ‘bp_before_activity_entry’, ‘custom_markup_before_every_fifth_activity’ );`
    it show ads after every 5 activity but has a little problem when I went from “all members” to “friends” and the activity feed didn’t load. I have to refresh the page to get it to load.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20793

    Hi Lucas,
    Welcome to BuddyDev and thank you for asking the question.

    You don’t need the above code, There is a simple and better way to do it. Please allow me to write a blog post and link it here.

    Since this seems to be beneficial for many people, I am going to write a post with details and link here.
    Will be linking in next 24 hours.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    lucas on #20808

    thanks brajesh i will be waiting for it

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
  • Participant
    Level: Initiated
    Posts: 5
    lucas on #20811

    thanks brajesh for the code but problem still exist when you click on my friend activity from actvity stream or use any filter the actvity stream don’t load you have to refresh it.

  • Participant
    Level: Initiated
    Posts: 5
    lucas on #20812

    the problem start when you start using adsense code otherwise it work smoothly

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20818

    Hi Lucas,
    Thank you.
    Can you please share the code and the answer to following question.

    1. Is it happening in all cases if you use adsense?
    2. or is it only happening on loading new activities(load more/filters)

    Did you inject the adsense library code in header before adding the ads snippet?

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    lucas on #20821

    thanks for the reply
    well i customize a bit but i have also tried it with the original code you gave but problem still exist. well the problem is that when you are on site wide activity stream when you click on my friend activity it shows a blank page and until you refresh
    ok i am sharing the code i am using.

    
    function buddydev_inject_content_after_n_activities() {
        static $current_activity_index = 1;
     
        // change $n to the correct number of activities.
        $n = 5; // After how many activities you want to inject content.
     if ( !is_user_logged_in()){   //i added it to show ads only to non-logged in user
        if ( $current_activity_index % $n === 0 ) {
    		 ?>
                <div class="activity-avatar">
    		
    		<div class="names"style="color:#c92e2e;">Sponsored</div>
    			<div class="nices-name">@sponsored</div>
    		</div>
    			<div class="activity-content">
    			<div class="ads"><script async adsense code //this the adsense script
    </script></div>
            </div><?php
            // show the activity injected content.
        }}
        // increment the current index.
        $current_activity_index ++;
     
    }
     
    add_action( 'bp_after_activity_entry', 'buddydev_inject_content_after_n_activities' );
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20822

    Hi Lucas,
    I am guessing it is only happening with adsense.
    Adsense does not seem to be supported in content loaded via ajax. You will most probably need to do extra works for it

    https://support.google.com/admanager/answer/2694377

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved