GUIDE: How to create your very own activity feed template from scratch(almost)
-
For the most part, Buddypress is great! However, as a theme/php developer, its great strengths (ease if use and installation) also become its weakness. Customizing the Activity feed beyond CSS (ie: building your own template), the labyrinth of procedural code, the dated codex, and the hooks and actions becomes quite the challenge.
Now, if you just want to customize the title, change colors, or modify the layout a lot of that can be achieved by CSS.
But if you want to access activity data beyond it’s intended scope (say, for mobile app development, API layers, or just becasue you want to get your hands on it!) then this guide is for you!!
Prerequisites:
Basic PHP Knowledge
WP Theme development experience (even child-theme)
Experience using the fabled ‘functions.php’ and template tags.Ok, so if you meet the prerequisites allow me to share with you the best bp function ever (well, maybe not ever, but the best one for me, right now, haha)
take a look at
/wp-content/plugins/buddypress/bp-activity/bp-activity-functions.php
–line:900ish
and say hello to pandoras box!/** * Retrieve an activity or activities. * * bp_activity_get() shares all arguments with BP_Activity_Activity::get(). The * following is a list of bp_activity_get() parameters that have different * default values from BP_Activity_Activity::get() (value in parentheses is * the default for the bp_activity_get()). * - 'per_page' (false) * * @since BuddyPress (1.2) * * @see BP_Activity_Activity::get() For more information on accepted arguments * and the format of the returned value. * @uses wp_parse_args() * @uses wp_cache_get() * @uses wp_cache_set() * @uses BP_Activity_Activity::get() {@link BP_Activity_Activity} * @uses apply_filters_ref_array() To call the 'bp_activity_get' hook. * * @param array $args See BP_Activity_Activity::get() for description. * @return array $activity See BP_Activity_Activity::get() for description. */ function bp_activity_get( $args = '' ) { $defaults = array( 'max' => false, // Maximum number of results to return 'page' => 1, // page 1 without a per_page will result in no pagination. 'per_page' => false, // results per page 'sort' => 'DESC', // sort ASC or DESC 'display_comments' => false, // false for no comments. 'stream' for within stream display, 'threaded' for below each activity item 'search_terms' => false, // Pass search terms as a string 'meta_query' => false, // Filter by activity meta. See WP_Meta_Query for format 'show_hidden' => false, // Show activity items that are hidden site-wide? 'exclude' => false, // Comma-separated list of activity IDs to exclude 'in' => false, // Comma-separated list or array of activity IDs to which you want to limit the query 'spam' => 'ham_only', // 'ham_only' (default), 'spam_only' or 'all'. /** * Pass filters as an array -- all filter items can be multiple values comma separated: * array( * 'user_id' => false, // user_id to filter on * 'object' => false, // object to filter on e.g. groups, profile, status, friends * 'action' => false, // action to filter on e.g. activity_update, profile_updated * 'primary_id' => false, // object ID to filter on e.g. a group_id or forum_id or blog_id etc. * 'secondary_id' => false, // secondary object ID to filter on e.g. a post_id * ); */ 'filter' => array() ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); // Attempt to return a cached copy of the first page of sitewide activity. if ( 1 == (int) $page && empty( $max ) && empty( $search_terms ) && empty( $meta_query ) && empty( $filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) { if ( !$activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ) ) { $args = array( 'page' => $page, 'per_page' => $per_page, 'max' => $max, 'sort' => $sort, 'search_terms' => $search_terms, 'meta_query' => $meta_query, 'filter' => $filter, 'display_comments' => $display_comments, 'show_hidden' => $show_hidden, 'spam' => $spam ); $activity = BP_Activity_Activity::get( $args ); wp_cache_set( 'bp_activity_sitewide_front', $activity, 'bp' ); } } else { $args = array( 'page' => $page, 'per_page' => $per_page, 'max' => $max, 'sort' => $sort, 'search_terms' => $search_terms, 'meta_query' => $meta_query, 'filter' => $filter, 'display_comments' => $display_comments, 'show_hidden' => $show_hidden, 'exclude' => $exclude, 'in' => $in, 'spam' => $spam ); $activity = BP_Activity_Activity::get( $args ); } return apply_filters_ref_array( 'bp_activity_get', array( &$activity, &$r ) ); }
This magical function, dubbed
bp_activity_get()
will allow you to get just about every piece and part of an activity feed even OUTSIDE of a bp loop. Not only that, but it allows for you to runfilters
through it, meaning you can grab activities by group, by member, sitewide, or just ONE activity if you want.Here’s a sample function I made that grabs the 8 most recent activities and puts the keys I found important into an array. If you’re familiar with the wp $post object, this is quite similar:
/** * Dumps a list of the latest activities. May be used with pagination by setting $current_page * * @posts_per_page INT, Default is 8. * @current_page INT, which page you are on, Default is 1. * * @var_dump ARRAY, activity items. */ function get_latest_activities($posts_per_page=8,$current_page=1) { $args = array ( 'page' => $current_page, 'per_page' => $posts_per_page ); $pre_rec = bp_activity_get($args); $activities = $pre_rec['activities']; $results = array(); foreach ($activities as $activity) { $record = array( 'ID' => strip_tags($activity->id), 'TYPE' => strip_tags($activity->type), 'TIME' => strip_tags($activity->date_recorded), 'AUTHOR' => strip_tags($activity->display_name), 'TITLE' => strip_tags($activity->action), 'CONTENT' => strip_tags($activity->content), 'ITEM_ID' => strip_tags($activity->item_id), 'REL_ID' => strip_tags($activity->secondary_item_id), ); array_push($results, $record ); unset($record); } foreach ($results as $record) { var_dump ($record); print "<br>###################<br>"; } }
So now, I could place any piece of the data, anywhere I want. You could also (if you wanted to) create template tags that are much more custom. You can also get the data outside of the loop, throw it in the <head> section for SEO purposes, and on and on and on. Maybe you want to throw a widget on your site with the latest activities? This method will do it.
Let me know if you have any questions, I hope this helps, because it took me way too long to figure this out and the current documentation did not help.
- The topic ‘GUIDE: How to create your very own activity feed template from scratch(almost)’ is closed to new replies.