Opened 6 years ago
Closed 6 years ago
#7988 closed enhancement (fixed)
Supply new activity id to bp_activity_add action hook
Reported by: | drywallbmb | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.0 | Priority: | normal |
Severity: | normal | Version: | 3.0.0 |
Component: | Activity | Keywords: | |
Cc: |
Description
The bp_activity_add
action fired at https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/bp-activity-functions.php#L1925 will only supply the ID of the just-added activity if that ID was present when the function was called — which is to say, it's only present if an activity is actually being edited, not added.
Not knowing the ID of the just-added activity severely limits the utility of this action.
Example use case: I want to add certain meta
value to activities as they're added. Hooking into bp_activity_add
seems like the right (and possibly only) place to watch for these additions, but when activities are added I need to then call other code to actually fetch the ID of the activity in question in order to give it a meta value. This code is cumbersome to write and results in an unnecessary performance hit as I need to query the DB to get an ID that could otherwise just be provided to me.... the action provides all other useful and relevant information about the new activity, just not its id.
Attachments (1)
Change History (11)
#2
@
6 years ago
Hi @drywallbmb - Thanks for the ticket!
Your second option looks more consistent with how we pass IDs elsewhere in BP. Please do write a patch!
#3
follow-up:
↓ 4
@
6 years ago
You might also want to consider using the 'bp_activity_after_save'
hook, which will include the activity ID:
https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/classes/class-bp-activity-activity.php#L304
#4
in reply to:
↑ 3
;
follow-up:
↓ 7
@
6 years ago
Replying to r-a-y:
You might also want to consider using the
'bp_activity_after_save'
hook, which will include the activity ID:
https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/classes/class-bp-activity-activity.php#L304
Yep, I'd hoped to use that one! But there's no guarantee that the code invoking bp_activity_add
will then also do bp_activity_after_save
. (For example, bbPress calls bp_activity_add()
directly but then never invokes that after-save hook anywhere.)
#5
@
6 years ago
Just uploaded a patch... Made a wild guess at target release version in the phpdoc. Also, haven't done a patch in SVN in forever, let me know if I borked it and I can attempt to fix.
#7
in reply to:
↑ 4
@
6 years ago
Replying to drywallbmb:
Yep, I'd hoped to use that one! But there's no guarantee that the code invoking
bp_activity_add
will then also dobp_activity_after_save
. (For example, bbPress callsbp_activity_add()
directly but then never invokes that after-save hook anywhere.)
You are guaranteed to get the activity ID if you use the 'bp_activity_after_save'
hook even for new activity items.
Please try the 'bp_activity_after_save'
hook in your custom code.
I can envision two different approaches to addressing this.
One would be to update $r before the action is called:
$r['id'] = $activity->id;
Another would be to pass a second parameter to the action:
do_action( 'bp_activity_add', $r, $activity->id );
I've no idea which would be preferable or more appropriate, but if someone can provide guidance I'd be happy to submit a patch!