Skip to:
Content
Pages
Categories
Search
Top
Bottom

custom members loop


  • mykrabuilding
    Participant

    @mykrabuilding

    Hi guys, ive managed to almost get where I need to go with this thanks to the great explanations and code examples on these forums but Im stuck at the final hurdle! Basically what Im trying to create is an “in/out” board on our intranet which is a single page on the site which loops through all users and displays their last status update. All our staff will then use the status update function to update their whereabouts, eg “at lunch back at 2” or “meeting offsite until 3”
    Ive managed to create a custom page template with a hacked up version of the members loop which so far gives me the avatar, username and last status update, but the final thing I need is the time/date of the last status update instead of the time the user was last active.
    I could do this in the activity loop and Ive created a function in my functions.php to format the date the way I want it, but it seems this does not apply to the members loop.

    here is the code I am using in my page template:

    <?php
    /*
    Template Name: In Out Page
    */
    ?>
    <?php get_header(); ?>
    <div class="sleeve_main">
    	<div id="main">
    		<h2><?php the_title(); ?></h2>
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
        <?php do_action( 'bp_before_directory_members_list' ); ?>
      <ul id="members-list" class="item-list" role="main">
      <?php while ( bp_members() ) : bp_the_member(); ?>
         <li>
          <div class="item-avatar">
             <?php bp_member_avatar(); ?>
             <?php bp_member_name(); ?>
          </div>
           <div class="item">
            <div class="item-title">
                <?php if ( bp_get_member_latest_update() ) : ?>
                   <span class="update"><?php bp_member_latest_update(array( 'view_link' => false )); ?></span>
                <?php endif; ?>
            </div>
            <div class="item-meta"><span class="activity"><?php bp_member_last_active(); ?></span></div>
            <?php do_action( 'bp_directory_members_item' ); ?>
            </div>
            <div class="action">
                <?php do_action( 'bp_directory_members_actions' ); ?>
           </div>
           <div class="clear"></div>
       </li>
     <?php endwhile; ?>
     </ul>
     <?php do_action( 'bp_after_directory_members_list' ); ?>
     <?php bp_member_hidden_fields(); ?>
    <?php else: ?>
       <div id="message" class="info">
          <p><?php _e( "Sorry, no members were found.", 'buddypress' ); ?></p>
       </div>
    <?php endif; ?>
    		</div> <!-- main -->
    </div> <!-- sleeve -->
    <?php get_footer(); ?>

    and this is the code to format the date in my functions.php :

    function format_activity_date() {
      $activityDate=bp_get_activity_date_recorded();
      // Get GMT offset from root blog
      $root_blog_offset = get_blog_option( BP_ROOT_BLOG, 'gmt_offset' );
      // Calculate offset time
      $time_offset = $time + ( $root_blog_offset * 3600 );
      // Format the time using the offset and return it; date-i18n retrieves the date in localized format
      return '' . date_i18n("l, d/m/y, g:ia", strtotime($activityDate) + $time_offset) . '';
    }
    add_filter('bp_activity_time_since', 'format_activity_date');

    from what I can tell, I dont want to be using the bp_member_last_active() function as this will return the last time the user did anything. Rather, I need to use some other function which will return the timestamp of the users last status update, but I cant get this part to work!
    Any help much appreciated
    cheers

Viewing 15 replies - 1 through 15 (of 15 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @mykrabuilding

    To my knowledge, the time of the latest update isn’t recorded. The latest update info is stored in user meta. To get it you’d do something like:

    $update = get_usermeta( $user_id, 'bp_latest_update' );

    You then have the option to get either the activity ID or the content (no timestamp I’m afraid).

    echo $update['id'];
    echo $update['content'];

    You can of course add your own user meta when somebody writes a status update. You could record a timestamp and then fetch it as described above.

    See:
    https://codex.wordpress.org/Function_Reference/update_user_meta

    I think bp_activity_posted_update is the hook you’ll need.


    mykrabuilding
    Participant

    @mykrabuilding

    Thanks for your quick reply Henry!
    What youre saying makes sense however Im sure there must be a way to do this! When I look in the mysql database theres a table named bp_activity in which I can see the users status updates stored with type “activity_update”. In another column there is the date recorded. The member loop must be pulling the text for the last status update from this table, I just need to figure out how to get it to also pull the date out so I can use it.


    Henry Wright
    Moderator

    @henrywright

    You’re right! My apologies! So that makes it much easier!

    You can get the time recorded by doing something like this:

    $update = get_usermeta( bp_get_member_user_id(), 'bp_latest_update' );
    $activity = bp_activity_get_specific( array( 'activity_ids' => $update['id'] ) );
    $activity = $activity[0];
    $time = $activity->recorded_time;
    echo $time;

    Note: This is intended to work inside the members loop – if you want to use it outside then the bp_get_member_user_id() function will need to be changed.


    mykrabuilding
    Participant

    @mykrabuilding

    Thanks again @henrywright!
    unfortunately being only intermediate with php and new to buddypress Im still having trouble getting this to work. Ive tried creating a new function with the code you provided and calling it from my template page, also tried including the code directly into the template but no luck so far. Any other tips you could give me?
    Cheers


    Henry Wright
    Moderator

    @henrywright

    Are you using it within the loop? i.e. within while and endwhile?


    mykrabuilding
    Participant

    @mykrabuilding

    i think i am! ive created a function in functions.php as follows:

    /**
      * get user last update timestamp
      */
    function get_user_last_update_timestamp() {
    	$update = get_usermeta( bp_get_member_user_id(), 'bp_latest_update' );
    	$activity = bp_activity_get_specific( array( 'activity_ids' => $update['id'] ) );
    	$activity = $activity[0];
    	$time = $activity->recorded_time;
    	echo $time;
    }

    and im calling it from my page template (which contains a stripped down version of the code from the members-loop.php file) as follows:

    <?php
    /*
    Template Name: In Out Page
    */
    ?>
    <?php get_header(); ?>
    <div class="sleeve_main">
    	<div id="main">
    		<h2><?php the_title(); ?></h2>
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
        <?php do_action( 'bp_before_directory_members_list' ); ?>
      <ul id="members-list" class="item-list" role="main">
      <?php while ( bp_members() ) : bp_the_member(); ?>
         <li>
          <div class="item-avatar">
             <?php bp_member_avatar(); ?>
             <?php bp_member_name(); ?>
          </div>
           <div class="item">
            <div class="item-title">
                <?php if ( bp_get_member_latest_update() ) : ?>
                   <span class="update"><?php bp_member_latest_update(array( 'view_link' => false )); ?></span>
                <?php endif; ?>
            </div>
            <div class="item-meta"><span class="activity"><?php get_user_last_update_timestamp(); ?></span></div>
            <?php do_action( 'bp_directory_members_item' ); ?>
            </div>
            <div class="action">
                <?php do_action( 'bp_directory_members_actions' ); ?>
           </div>
           <div class="clear"></div>
       </li>
     <?php endwhile; ?>
     </ul>
     <?php do_action( 'bp_after_directory_members_list' ); ?>
     <?php bp_member_hidden_fields(); ?>
    <?php else: ?>
       <div id="message" class="info">
          <p><?php _e( "Sorry, no members were found.", 'buddypress' ); ?></p>
       </div>
    <?php endif; ?>
    		</div> <!-- main -->
    </div> <!-- sleeve -->
    <?php get_footer(); ?>

    Henry Wright
    Moderator

    @henrywright

    I think $update['id'] could be wrong. Can you try $update[0][O] instead?

    I’m not at my computer now until tomorrow morning so can’t test.


    mykrabuilding
    Participant

    @mykrabuilding

    thanks henry – gave it a crack and unfortunately got: Fatal error: Cannot use string offset as an array
    appreciate your help! ill keep trying to wrap my head around it and let you know if I work it out
    cheers


    mykrabuilding
    Participant

    @mykrabuilding

    still struggling with this one!


    mykrabuilding
    Participant

    @mykrabuilding

    i noticed the “buddypress sitewide activity widget” plugin achieves what I want to do but I cant figure out how despite crawling through the code for hours…


    Henry Wright
    Moderator

    @henrywright

    Hi @mykrabuilding

    I’ve now had a chance to do this at my PC. Just tested this so it should work:

    $update = get_usermeta( bp_get_member_user_id(), 'bp_latest_update' );
    $activity = bp_activity_get_specific( array( 'activity_ids' => $update['id'] ) );
    $activity = $activity['activities'][0];
    echo $activity->date_recorded;

    mykrabuilding
    Participant

    @mykrabuilding

    thanks again Henry! this is working but its pulling the same date for the two different users Im currently displaying. I dont feel right about using up your time for free, so Im talking to the boss about whether I can get a budget to spend on getting this coded properly. Are you up for freelance work?
    cheers


    Henry Wright
    Moderator

    @henrywright

    @mykrabuilding could it be that the two members you’re displaying haven’t made an activity update yet? Just a thought! If that is the case then it’ll just need a simple check introduced to see if the member has made an activity update – if they haven’t then display nothing, if they have then display the date.


    Henry Wright
    Moderator

    @henrywright

    Try this:

    $update = get_usermeta( bp_get_member_user_id(), 'bp_latest_update' );
    if ( is_array( $update ) ) {
        $activity = bp_activity_get_specific( array( 'activity_ids' => $update['id'] ) );
        $activity = $activity['activities'][0];
        echo $activity->date_recorded;
    }

    Are you up for freelance work?

    I don’t think that will be needed as I think this should work now.


    Henry Wright
    Moderator

    @henrywright

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘custom members loop’ is closed to new replies.
Skip to toolbar