BuddyDev

Search

Inject content into BuddyPress Activity stream after a certain number of posts

  • Participant
    Level: Initiated
    Posts: 2
    Daniel on #34856

    After reading through the following post :

    https://buddydev.com/inject-content-into-buddypress-activity-stream-after-a-certain-number-of-activities/

    Which goes over how to have content/ad appear after every x number of activities in the feed.
    I’ve been trying to manipulate the code to only show the content/ad after n activity.

    ie. the content only shows up once after the x post not infinite times after every x number of posts.

    Does anyone know the code for this?

  • Participant
    Level: Initiated
    Posts: 2
    Daniel on #34857

    This is the current code that makes content appear infinite times every n posts. How would i change this to make the content only come up once at a specified number of posts?? thanks

    ‘/**
    * Inject content into BuddyPress Activity stream after ‘n’ activities.
    */
    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 ( $current_activity_index % $n === 0 ) {
    ?>
    <li class=’activity-item activity-ad-item activity-custom-entry’>
    Your code snippet …

    <?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: 24190
    Brajesh Singh on #34867

    Hi Daniel,
    Thank you for the question.

    The above code will not be able to help you achieve it.

    To make sure that the content appears only 1 time, you need to keep track if the content was shown on the current page.

    To do that you will need to add js code and track the view state by using cookie/local or session storage.

    are you comfortable writing js code? If yes, I will be glad to provide you the steps for achieving it.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Daniel on #34869

    Unfortunately im not comfortable in JS. Not really a coder tbh.
    If theres a way around writing code it would be appreciated.

    Otherwise I appreciate the quick response!

    Dan

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #34894

    Hi Dan,
    Thank you for the reply.

    I am sorry, I am unable to assist you with this due to lack of time. I do believe it is worth investigating and providing a solution in future.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 1
    Adam Pickney on #35722

    Hi Brajesh, I would be curious to know how to do this via js.

    I am trying to inject a widget area that shows up once after the 1st activity feed item only.

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #35745

    Hi Adam,
    Thank you for the question.

    The solution is to assign an id to each of the injected content to uniquely identify them. Then, set cookies to keep these ids for a small amount for time(say 1 hour).

    On the server side, you can check the cookie and avoid showing the old content.

    Hope that helps.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved