BuddyPress Activity As wire updated for BuddyPress 1.8, BuddyPress 1.9+
This is a much needed update for BuddyPress activity as Wire tutorial. Thank you all for your patience. As I said earlier, we are focusing again on BuddyDev and paying attention to what you all say, and here I am putting this update on your demand.
The goal of this tutorial is:-
- To use BuddyPress activity mention feature to emulate User wall/wire
- Without any change in the theme code( That's right )
To make BuddyPress Activity feature emulate the user wall/wire, we will go in three steps as below:-
- Step 1: Show the activity post form on other user's profile
- Step 2: Filter the text that says "What's new {username}" to say "Write something to {displayed_user_name}"
- Step 3: Remove BuddyPress function which handles the post update action and hook our custom function that allows us to save the activity posted from other user's profile as mention.
Here we go 🙂
Step 1: Sow the activity post form on other user's profile:-
In the code below, we are showing the activity post form on other user's activity/mention tab
1 2 3 4 5 6 7 8 9 | //step1: show activity post form on other user's profile //we can use the 'bp_before_member_activity_post_form' or 'bp_after_member_activity_post_form' action here function devb_aawire_show_post_form() { if ( is_user_logged_in() && bp_is_user() && !bp_is_my_profile() && (!bp_current_action() || bp_is_current_action('just-me') || bp_is_current_action('mentions') ) ) bp_locate_template(array( 'activity/post-form.php' ), true); } add_action( 'bp_after_member_activity_post_form', 'devb_aawire_show_post_form' ); |
That will show the form as shown in the previous image.
Step 2: Filter the text that says "What's new {username}" to say "Write something to {displayed_user_name}"
In the code below, we are filtering using the gettext filter.
1 2 3 4 5 6 7 8 9 10 | function devb_aawire_translate_whats_new_text( $translated_text, $text, $domain ) { if ( $text == "What's new, %s?" && $domain == 'buddypress' && !bp_is_my_profile() && bp_is_user() ) { $translated_text = sprintf( __( "Write something to %s?", 'buddypress' ), bp_get_displayed_user_fullname() ); } return $translated_text; } add_filter( 'gettext', 'devb_aawire_translate_whats_new_text', 10, 3 ); |
Step 3: Remove BuddyPress function which handles the post update action and hook our custom function that allows us to save the activity posted from other user's profile as mention.
So, all we need to do is remove the default handler for the post form and use our own. Here is the code that checks and removes the default handler and adds our own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //Step3: Remove buddypress functions that save the activity update and hook our custom update function function devb_aawire_update_activity_posting_hooks() { //pre 1.7 themes that copied it if ( has_action( 'wp_ajax_post_update', 'bp_dtheme_post_update' ) ) remove_action( 'wp_ajax_post_update', 'bp_dtheme_post_update' ); //if the theme is using bp1.7+ template pack support if (has_action('wp_ajax_post_update', 'bp_legacy_theme_post_update')) remove_action( 'wp_ajax_post_update', 'bp_legacy_theme_post_update' ); //add our own handler for activity posting add_action( 'wp_ajax_post_update', 'devb_aawire_post_update' ); } add_action( 'init', 'devb_aawire_update_activity_posting_hooks' ); |
As you see above, we will need to write our custom function 'devb_aawire_post_update' to handle the activity post action.
Here is the code for that function:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | /* AJAX update posting */ function devb_aawire_post_update() { global $bp; $is_wire_post = false; /* Check the nonce */ check_admin_referer( 'post_update', '_wpnonce_post_update' ); if ( !is_user_logged_in() ) { echo '-1'; exit(0); } if ( empty( $_POST['content'] ) ) { echo '-1<div id="message"><p>' . __( 'Please enter some content to post.', 'buddypress' ) . '</p></div>'; exit(0); } if ( empty( $_POST['object'] ) && function_exists( 'bp_activity_post_update' ) ) { //this is what I have changed if ( !bp_is_my_profile() && bp_is_user() ) { $content = '@' . bp_get_displayed_user_username() . ' ' . $_POST['content']; $is_wire_post = true; } else { $content = $_POST['content']; } //let us get the last activity id, we will use it to reset user's last activity $last_update = bp_get_user_meta( bp_loggedin_user_id(), 'bp_latest_update', true ); if ( $is_wire_post ) add_filter( 'bp_activity_new_update_action', 'devb_aawire_filter_action' ); $activity_id = bp_activity_post_update( array('content' => $content) ); if ( $is_wire_post ) { remove_filter( 'bp_activity_new_update_action', 'devb_aawire_filter_action' ); //add activity meta to remember that it was a wire post and we may use it in future if ( $activity_id ) bp_activity_update_meta( $activity_id, 'is_wire_post', 1 ); //let us remember it for future</pre> //for 2.0 Let us add the mentioned user in the meta, so in future if we plan eo extend the wall beyond mention, we can do that easily bp_activity_update_meta ( $activity_id, 'wire_user_id', bp_displayed_user_id() ); //let us remember it for future } //reset the last update bp_update_user_meta(get_current_user_id(), 'bp_latest_update', $last_update); //end of my changes } elseif ( $_POST['object'] == 'groups' ) { if ( !empty($_POST['item_id'] ) && function_exists( 'groups_post_update' ) ) $activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $_POST['item_id'] ) ); } else { $activity_id = apply_filters( 'bp_activity_custom_update', $_POST['object'], $_POST['item_id'], $_POST['content'] ); } if ( !$activity_id ) { echo '-1<div id="message"><p>' . __( 'There was a problem posting your update, please try again.', 'buddypress' ) . '</p></div>'; exit(0); } if ( bp_has_activities( 'include=' . $activity_id ) ) : ?> <?php while ( bp_activities() ) : bp_the_activity(); ?> <?php bp_locate_template( array( 'activity/entry.php' ), true ) ?> <?php endwhile; ?> <?php endif; exit(0); } //filters activity action to say posted on xyz's wall function devb_aawire_filter_action( $action ) { $action = sprintf( __( '%s posted on %s\'s wall', 'buddypress' ), bp_core_get_userlink( get_current_user_id() ), bp_core_get_userlink(bp_displayed_user_id() ) ); return $action; } |
Step 4: On BuddyPress 2.0+, we need to filter the activity action to say wall post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //for BuddyPress 2.0 dynamic action string, we need to filter the update action and see if it is a wire/wall post add_filter( 'bp_activity_new_update_action', 'devb_aawire_update_activity_action', 10, 2 ); function devb_aawire_update_activity_action( $action, $activity ) { //check if this is a wall post? if ( !bp_activity_get_meta( $activity->id, 'is_wire_post', true ) ) return $action; //if we are here, It must be a wire post //since bp 2.0, I have added a meta key to store the user id on whose wall we are posting if ( !$user_id = bp_activity_get_meta( $activity->id, 'wire_user_id', true ) ) { //before 2.0, since the id did not exist, we will be using the @mention finding username $usernames = bp_activity_find_mentions( $activity->content ); if ( is_array( $usernames ) ) $usernames = array_pop( $usernames ); if ( $usernames ) $user_id = bp_core_get_userid( $usernames ); } if ( !$user_id ) //we don't have info about the person on whose wall this poat was made return $action; //if we are here, let us say something nice really nice $action = sprintf( __( '%s posted on %s\'s wall', 'buddypress' ), bp_core_get_userlink( $activity->user_id ), bp_core_get_userlink( $user_id ) ); return $action; } |
Now, if a user posts, the activity is like this
If you read through the code, It should be self explanatory. Still, we are using $is_wire_post variable to track if the current post is made as wire post. If it is wire post, we filter the activity action to say User 1 posted on User2's wall. You can put all the above code in bp-custom.php or your theme's functions.php
And to disable the box that says post in, we will need following css
1 | body.activity.bp-user #whats-new-post-in-box{ display:none;} |
The above css code hides the drop down option that shows where to post( My profile/Groups etc).
Here is a surprise gift for you if you made till here. The BuddyPress Activity as wire is available as a plugin now and you can just drop in the plugin and activate. No need for any theme modification, except one line of css.
Download & Installation:-
https://buddydev.com/plugins/bp-activity-as-wire/
We will need to make 1-2 small change when BuddyPress 2.0 comes out ( The above code will work fine but we can utilize the dynamic activity action generation in a much better way).
Ok, Now it is your turn to let me know if it is working for you or not and what enhancements should I make?
It's working for me with a standard WP theme but not with the Salutation theme I'm using. Step 2 (Filter the text that says “What’s new {username}” to say “Write something to {displayed_user_name}”) isn't working with that premium theme. The functionality itself is here, only the what's new text isn't changed.
Thank you Hans.'
That means they are not using buddypress as text domain as I have used in the plugin. you can change this line
[sourcecode language="php"]
if( $text == "What's new, %s?" && $domain == 'buddypress' && !bp_is_my_profile() && bp_is_user() ){
[/sourcecode]
to [sourcecode language="php"]
if( $text == "What's new, %s?" && !bp_is_my_profile() && bp_is_user() ){
[/sourcecode]
and that will work.
I next version I will remove the text domain check too. I used it to avoid any collision with other plugins, but after a second thought, It seems the text is not that much generic.
This change did not the job with Salutation. I didn't had the time to debug why, but Salutation is sometimes a real brain twister. 😉
@Brajesh
I am not able to see the post in user activity. When I am posting it on his/her wall, Its refreshing the activity list. Gr8. That's fine. But when I go somewhere else and return back. There is not post posted by me on their wall. It is on activity page but not on the user activity page. And even the
devb_aawire_filter_action
is not working. I am getting the activity header as default.Hi Mehul,
1. The posts will appear under the mentions tab of activity.
2. Are you using BuddyPress 2.0, I have to put a fix for BP 2.0.
[sourcecode language="php"]
// Remove post in box from activity wire other user posting activity post form
function remove_activity_post_in_box() {
body.activity.bp-user #whats-new-post-in-box{ display:none;}
}
add_action ('bp_head', 'remove_activity_post_in_box');
[/sourcecode]
@sbrajesh, i think you can use it directly in the plugin so no need to add anything in the style sheet by the user.
Note : I dont know why the style opening and closing tag is not showing on the above code.
thanks
Thank you Abbey,
I have updated the code. I did not think about including it directly in the page but I believe, we can give it a shot 🙂
@sbrajesh,
see the complete code below as the above is incomplete.
http://pastebin.com/wiDwdBdr
Regards
Thank you Abbey 🙂
@sbrajesh,
Please when should we be expecting update of this tutorial for buddypress 2.0.
This has stopped working.
Regards
Hi Abbey,
Thank you for the comment. I have updated the tutorial as well as the plugin for BuddyPress 2.0 now 🙂
Thank you
Brajesh
@sbrajesh,
Thanks very much thats why i like you. always on point. you are the best.
I am current building a theme that i want to release to the community for free.
So this tutorial is very important for me and I will be buying your updated facebook like activity stream plugin soon. i have the old one which i got when i was still a full member.
thank you
Hi Abbey,
Thank you for the kind words 🙂
I am looking forward to your theme, please do let me know if I can be of any help. Thank you for the interest in Facebook Like activity stream( It is now compatible with BuddyPress followers plugin too ).
Regards
Brajesh
Brajesh, can you check this support forum post I created regarding a possible enhancement for Wire https://buddydev.com/support/forums/topic/segregate-wall-posts-from-the-main-activity-stream-in-bpaaw/
Great plugin! I was wondering if there was a way to only allow friends to post on wall? And also have @mentions still work?
This is nice. However, it made the "All Member Notices" show up at the very bottom of the page on or below the footer.
Hi Derddy,
I just tested and I don't see any notice. are you using the plugin version or the code from the above post. Please use the plugin from here.
https://buddydev.com/plugins/bp-activity-as-wire/
Thanks
Brajesh
Does this work with the Kleo theme? I a new to wordpress. Do I have to put in an y coding myself or are you just explaining the coding it took to make this work?
Hi,
I am not sure if it works with Kleo theme or not but you can install the plugin and test it. It is available from here.
https://buddydev.com/plugins/bp-activity-as-wire/
Please do let me know if this works with Kleo or not?
Any plans on updating this nice sweet little plugin to current version of WP and Buddypress versions?
Thanks
Kind regards
Joans.
Hi Joans,
I haven't seen any breaking changes in recent BuddyPress versions. Are you seeing any issue?
Thank you
Brajesh
Hello! sorry for my English. I'm doing my site caundo DIV and use this plugin footer columns are cluttering. No way to fix it?
I am sorry but I am not able to help. Most probably some conflict with the theme layout.