Saturday, August 16, 2014

Displaying custom buddypress messages in a custom template

Buddypress plugin is completely free and it works out of the box for a community website that lets users interact easily. But after going through my clients requirements, I learned that most of the functionality he was up to is not provided in Buddypress by default. So I need to Google around to find out if those custom functions are possible.


Good thing to know that Buddypress support has a huge based community of contributors and users, and I spotted some old and recent topics related to my clients custom specific requirements.
In this post, I would like to share with you a simple way how to display a custom Buddypress messages.  In my client’s request, he wants to display 5 recent and unread messages within the custom template I made where I display a members grid view.
The idea is to create a floating messages widget (I might include a screenshot soon) inside the members page.
If you think this is something similar to your needs, keep on reading.

First, you need to familiarize the inside and out for the messages component. Then examine the private messages loop and see the lines of codes to understand how to display the template tags you wanted to show in your custom messages widget.

What I did was very simple. I just copied the standard loop and apply css for my design.
<?php if ( is_user_logged_in() ) { ?>
                <div style=" background: none repeat scroll 0 0 rgb(255, 255, 255);
                                                height: auto;
                                                margin-bottom: -184px;
                                                margin-left: 760px;
                                                margin-top: 40px;
                                                padding: 5px;
                                                width: 40%;"
                                                id="">
                                              
                                <h4 style="text-align:center">Messages</h4>
                                <?php if ( bp_has_message_threads( 'max=6&amp;amp;per_page=1' ) ) : ?>
                                  <ul id="dns-message-threads">
                                  <?php while ( bp_message_threads() ) : bp_message_thread(); ?>
                               
                                                <li>
                                                  <!-- Example template tags you can use -->
                                                  <?php //bp_message_thread_id() ?>
                                                  <?php bp_message_thread_has_unread() ?>
                                                  <?php //bp_message_thread_unread_count() ?>
                                                  <?php //bp_message_thread_avatar() ?>
                                                  <span style="color:#da389f!important;font-weight:bold"><?php bp_message_thread_from() ?></span>
                                                  <?php //bp_message_thread_last_post_date() ?>
                                                  <?php //bp_message_thread_view_link() ?>
                                                  <?php //bp_message_thread_subject() ?>
                                                  <?php bp_message_thread_excerpt() ?>
                                                  <?php //bp_message_thread_delete_link() ?>
                                                </li>
                                  <?php endwhile; ?>
                                  </ul>
                               
                               
                                <?php endif;?>
                </div>
              
                <?php } // end messages widget ?>

If you notice, I placed the loop inside the “is_user_logged_in” condition to avoid showing it on non logged in users. And then inside the sample template tags, I just commented out those tags that I don’t want to display.
Also, consider looking thoroughly on the accepted parameters for various way to filter the members you wanted to showed up. 

No comments:

Post a Comment