Sabuj Kundu 1st Apr 2019

BuddPress is the most social network plugin for WordPress. We Codeboxr has lots of plugin integration with buddyPress. While working with CBX Poll I was finding a way how can put extra fields in the buddyPress activity posting form so that I can integration the poll posting in the stream/timeline easily. After checking the buddyPress codebase I get some idea and finished that integration with CBX Poll plugin. In this blog post I want to share my knowledge.

The Plan

At first let’s clear the plan what we are going to do:

  • Show Extra fields in buddyPress activity post form
  • Catch the submission and do extra tasks

Show Extra fields in buddyPress activity post form

BuddyPress has two themes: bp-legacy and bp-nouveau in the bp-templates folder. legacy theme is more php oriented and nouveau has more advance js based template or theme rendering but both has some common hooks as standard. To display extra fields in the form using php is not a good idea as per observation and pushing something using js will be better.
So I added js and css file in ‘add_action’ for “wp_enqueue_scripts” to register but didn’t enqueue which means js and css are not added to dom yet.
Later I used another hook “bp_before_activity_post_form” as “add_action” and from that callback method I enqueued the js and css that I registered from “wp_enqueue_scripts”. Though I did this tricks in many plugins and the only drawback in this way is css file may added in footer(we normally don’t want to enqueue css at footer) sometimes based on scope of hook.
So, when the js loads from hook “bp_before_activity_post_form” we know sure that it’s loading for screen buddyPress activity form and we can inject extra html to the form to show extra fields. BTW, you can use “wp_localize_script” to preload any html as js variable if you want.

Extra field names

I found that the legacy theme has some issues with field name, and your extra field names should not start with “whats-new” and any field name’s first char should not be underscore “_” which I found have no conflict with the latest theme nouveau. I am mentioning this specially becaused after working with nouveau when I tested my code with legacy theme I found it was not working then I needed to check the legacy theme’s js code ! BTW, if you add extra fields buddypress will submit those fields in ajax request while submitting the activity, so you don’t have to think about that part.

Catch the Balls!

The rest is catch the form submission. BuddPress submit the activity form as ‘user’, ‘group’ and ‘custom’ which they name as parameter ‘object’, we can ignore the custom part as I didn’t find it’s use yet but I think it’s kept for custom ways. For rest two ways after the data is saved we get the activity id (the primary key/field of the activity table). Now based on the object type buddyPress has two hooks “bp_activity_posted_update”, need three params($content, $user_id, $activity_id), “bp_groups_posted_update” needs four params($content, $user_id, $group_id, $activity_id), extra one is group id. So from those two hooks’ callback we can get the activity_id, $user_id and the text content $content. Now do as you need. Do I need to mention that from that callback method from $_POST variable you can get your extra field’s values value like $_POST[‘my_balls_count’] :P

How to Post in BuddyPress Stream/Activity from 3rd Party Plugin