Notify specific roles for specific forums
-
On my site, users with specific roles have access to specific forums. Is there a way that, for example, only users with the role of “Subscriber” can receive notifications when a new topic or reply is posted in a specific forum? Then I could set up another forum that only notifies users with the role of “Author”.
Let me know if that makes sense and/or is possible ??
Thanks!
-James
-
Hi James,
You can do what you want by hooking into the topic and reply recipients filters.
$recipients = apply_filters('bbpress_reply_notify_recipients', $recipients, $reply_id, $topic_id, $forum_id);
and
$recipients = apply_filters('bbpress_topic_notify_recipients', $recipients, $topic_id, $forum_id);
You’d have to figure out the logic of which forums notify which roles though.
Thanks for your response!
I know this may be asking a lot, but is there any way you could give an example of that function once it’s been hooked into — maybe with a fake user role and fake forum name?
It’s just hard for me to understand which variables to change and how to change them, as I’m not very good with php.
Thanks,
JamesIt’s not the case for variable changes. You’ll have to do the following:
- Create a meta-box containing all available roles to show in the new/edit forum screens.
- Hook into save_post to save the post_meta
- Create a function/method to fetch and return the saved roles from the post-meta
- Hook into the filters I mentioned above to use the saved roles
Have a look at this how-to on instructions creating the meta-box.
You can look at (and maybe even use)
bbPress_Notify_noSpam->_topic_recipients_inputfield()
to generate the checkboxes with the available roles in the meta-box (you might have to register the field name)Then finally try this:
function my_per_forum_notification( $recipients, $forum_id ) { $recipients = get_post_meta( $forum_id, 'the_key_used_during_save', $single = true ); return $recipients; } function my_topic_hook( $recipients, $topic_id, $forum_id ) { return my_per_forum_notification( $recipients, $forum_id ); } add_filter( 'bbpress_topic_notify_recipients', 'my_topic_hook', 10, 3 ); function my_reply_hook( $recipients, $reply_id, $topic_id, $forum_id ) { return my_per_forum_notification( $recipients, $forum_id ); } add_filter( 'bbpress_reply_notify_recipients', 'my_reply_hook', 10, 4 );
Note that I haven’t tested any of this, but it’s something along these lines.
Cheers,
VinnyHi Vinny,
I took care of this for James and noticed a bit of a bug (depending on interpretation). In your notify_new_topic (line 234) method, you have this conditional:if (0 === $forum_id) $forum_id = bbp_get_topic_forum_id($topic_id);
However, you don’t have the same conditional in notify_new_reply (line 301) and $forum_id and $topic_id are always 0 based on the way they’re called. Adding
if($forum_id === 0) { $forum_id = bbp_get_topic_forum_id($reply_id); }
would provided a valid forum_id to the resulting filters.
Thanks!
YaronHi Yaron,
Thanks for pointing that out.
You probably meant
bbp_get_reply_forum_id( $reply_id )
I looked at the code and it looks to me like the issue is actually elsewhere. I have the wrong parameters for the
bbp_new_reply
action set up.I’ll release a fix either later tonight or tomorrow, along with that
$forum_id
check.Version 1.7.2 has been released with the check for the forum_id and the fix for the parameters. With the parameter fix, I doubt we’ll have a $forum_id === 0, but I added the conditional anyway.
Thanks for bringing that up!
Nice! Happy to help ??
We would really like to do the same thing as James . . . so that we could have multiple forums – but notifications would go only to certain “roles” within out membership site.
If it’s not asking toooo much, — is there any chance that one of you might be able to elaborate just a bit further on this?
Sorry – I’m NOT a programmer, but any further hints would be greatly appreciated!!Hi Calie,
I’m assuming your question is for Yaron, as he’s the one who set up the custom code for James.
I’ll try to set up an add-on to do what you want asap, though.
That would be awesome! (if you get a chance to do an add-on)
Or – maybe we should discuss a paid project?
Ideally we need something for this:
3 groups: (we assign roles via JTadlock’s Members plugin), and have a forum for each:
– members (forum1)
– paid members (forum2)
– premium members (forum3)
Members can view only forum1
Paid members can view forum1 and forum2
Premium members can view forum1, forum2 and forum3Ideally, we want all New Topics for each forum to be sent to all members who can view that forum — (but not sent to anyone who can’t get into the forum anyway – so that we don’t send forum3 topics to anyone but Premium members – make sense?) — and it would definitely be preferred – if it goes to the correct members – “automatically” (so that we don’t rely on people remembering to choose which group to send to – before they hit “post” button).
Does that sound “doable”? — as an “add-on” or ?
Hi Callie,
I’ll look into building the add-on as other people might want it, too.
That would be awesome — if you end up getting a chance to do this!
Meanwhile — IF it is of any “use” or “importance” for you . . .
ABSOLUTELY understanding — that you are certainly not responsible for other plugins — just not sure how much it might or might not effect what you might be working on (or if other people would use a similar set up anyway)It is looking like we may need to try another plugin to help privatize different “groups” in bbPress (in addition to Members plugin) . . . . (or, at least, that is our current ‘argument’ – we’re not sure yet).
I’m only mentioning this to YOU — because, as near as I can tell, we’ll still need some way to send topic notification emails — to the “correct” people . . . whether they are defined by “role” (using Members) or by “group” (using Private Groups) . . . (or . . both)
. . . feel free to ignore all of that – if it has no significance for you. ??
Hi Calie,
We used the Advanced Custom Fields plugin and the Advanced Custom Fields User Role Selector plugin to add an “override new topic notifications” and “override reply notifications” fields to each forum post type. We then added the following code to our functions.php:/** * Helper function to get list of recipients for given forum ID and notification type * @param $type currently 'topic_override' or 'reply_override' * @param $forum_id * @return array */ function studiopie_get_recipients($type, $forum_id){ $topic_notify = get_field($type, $forum_id); $recipients = array(); foreach ($topic_notify as $opt_recipient) { if (! $opt_recipient) continue; $users = get_users(array('role' => $opt_recipient)); foreach ((array)$users as $user) { $user = get_object_vars($user); $recipients[$user['ID']] = $user['ID']; // make sure unique recepients } } return $recipients; } /** * Override new topic forum notifications */ function studiopie_topic_notify($recipients, $topic_id, $forum_id ){ $topic_override = get_field('topic_override',$forum_id); if($topic_override !== true) { return $recipients; } return studiopie_get_recipients('topic_notify', $forum_id); } add_filter( 'bbpress_topic_notify_recipients', 'studiopie_topic_notify', 10, 3 ); /** * Override reply forum notifications */ function studiopie_reply_notify( $recipients, $reply_id, $topic_id, $forum_id){ if($forum_id === 0) { $forum_id = bbp_get_topic_forum_id($reply_id); } $reply_override = get_field('reply_override',$forum_id); var_dump($reply_override); if($reply_override !== true) { return $recipients; } $recipients = studiopie_get_recipients('reply_notify', $forum_id); return $recipients; } add_filter( 'bbpress_reply_notify_recipients', 'studiopie_reply_notify', 10, 4 );
Yaron –
THANK you so much for posting this!
I’m not certain that I have the skills to make it work for us – but I am determined to try!
??
THANKS!On a slightly different path . . .
If we add “bbPress Private Groups” to our site – it becomes easy to set up private forums and allow certain roles to view / participate . . . but (according to the creator of Private Groups) we still need to add / change the bbpnns to somehow screen for:private_groups_get_forum_id_from_post_id($post_id, $post_type)
private_groups_can_user_view_post_id($forum_id). . . rather than choosing which ‘roles’ get notifications — before sending out “topic_notifications” . . . .
I just don’t have the programming skills to “hack” this out. :-\
Is this something that you might be able to easily help do?
(again – if this should be ‘privately’ asked, please accept my apologies, and let me know)Thank you!!
- The topic ‘Notify specific roles for specific forums’ is closed to new replies.