Skip Navigation

[Resolved] Send post form notification to all users with a specific role

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.56 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 3 voices.

Last updated by melodyM 1 year, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#2506845

I am trying to set up a notification that will send to all users of a specific role on the site.

I found this support ticket that Minesh worked on that looks like it provides a solution that works:

https://toolset.com/forums/topic/send-email-notification-to-user/

My one question from this is the part that says:

"3) Add following generic field to get all used based on specific role
a) add following field to your CRED form"

and then provides the code we need to use/modify.

What kind of generic field do I set up and where do I put the code? The code shows that it is a select field, but when I add a select field to add to the form, I'm not seeing where to put this code. Thanks for any assistance you can provide.

#2507403

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

I'm not sure that the solution in the other thread is what you are looking for, so let me clarify what that does, and you can confirm what it is you are aiming to do.

In the other thread the user wants to have a select dropdown shown in the form so that the visitor can choose a user that the form notification is subsequently sent to.

From your question it sounds like you want something different. I understand that you want to send a notification when a form submits that goes to all users that have some particular role.

Is that correct?

Form notifications are designed to be sent to the specific users or email addresses you specify in the form, but there is an API filter that can be used to modify the recipients, which would be the starting point for such a task: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

#2507599

In the thread that Minesh replied to that I linked to, the user asked:

--------------------------------
"Ideally, I would like the form to be able to dynamically pull in a list of users from a specif role (example: editors). It looks as though it is possible to "Use a shortcode instead of options ".
--------------------------------

So I think that is what we're looking for. The problem is I don't understand how to implement Minesh's solution. He says:

--------------------------------
"3) Add following generic field to get all used based on specific role

a) add following field to your CRED form
[cred_generic_field field="my_users" type="select" class="" urlparam=""]
{
"generic_type" : "user_id",
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[get-all-users role="administrator"]
}
[/cred_generic_field]
"
--------------------------------

But I'm not seeing anywhere to add a field in the form where I can have custom code like this. He also has custom code to add to the functions.php file. I know how to do that, so I should be fine if I can figure out how to add this field that he says to add. I'd just have to change the role from "administrator" to "Board Member" to meet our needs.

As for the link you provided, I'm not clear at all on what I need to do there. The code Minesh supplied to the other user is something I think I could succeed with, if I knew where to enter the field code he provided.

#2507963

Hello,

How do you setup the custom user role "Board Member"?
If you setup it with Toolset Access plugin, you can use the role slug "board_member", for example, use the custom shortcode, like this:
[get-all-users role="board_member"]

#2508007

I should be able to use that. I'm 99% sure I created the roles with Toolset Access, but it's been awhile since I set them up so I might not be remembering things correctly. I'll assume that I can use this and try it.

But then where would I put this shortcode to make it pull the e-mail addresses of all Board Member users to be the notification recipients? Would I still use the code Minesh included in the linked post in the functions.php file? Or will this shortcode do what's needed?

#2509395

Please provide a test site with the same problem, fill below private message box with test site credentials, also point out:
- the post form URL
- the problem page URL
- Where I can edit your custom PHP codes
I need a live website to test and debug it

#2510099

I have done below modifications in your website:
1) Edit snippet "notifications-to-board-members" as below:
hidden link

add_shortcode('get-all-users', 'get_all_users_data');
function get_all_users_data($attr) {
  
 if(isset($attr['role']) and $attr['role']!=''){
	$role = $attr['role'];
 }
 else{
    $role = 'administrator';
 }
 $blogusers = get_users( 'orderby=user_id&role='.$role);
 $arr = array();
 foreach ( $blogusers as $user ) {
  $arr[] = sprintf('{"value":"%1$s","label":"%2$s"}', $user->ID, $user->user_email);
 }
  $str = implode(',', $arr);
 
 return $str;
}

2) Edit the post form "SLICE Freelancer Application":
hidden link
a) In section "Form Editor", just above the submit button, add a generic select field "my_users", use the custom shortcode as below:
[get-all-users role="board_member"]
enable option: This field value is an user ID, and should be included in the list of available recipients for notifications

b) In section "Slice Application Form Submission",
enable option: Send notification to a WordPress user with an ID coming from a generic field in the form:
Choose above field: my_users

Please test again, check if it is fixed. thanks

#2510127

Question about this code. For testing purposes, is it doable to use

[get-all-users role="administrator"]

To see if the submission triggers the notification to be sent to all admin users? Then if that works, I'll change it back to

[get-all-users role="board_member"]

and assume that it will work for that until we are ready for that test

We don't want to send tests to board members yet. We're going to work some more on this application / voting system before we want to bring the board members in for assistance with testing. We have some additional functionality that we're going to try to achieve with this after this step is done.

This additional functionality may require submitting another job on your Toolset Contractors job board though. I'll see how far I can get with everything and if the remaining features I'm going to try to achieve are ones where you can assist or if they are ones where you'll recommend we get a contractor. I've submitted one job on the Job Board already but haven't gotten a response yet. But I haven't requested an estimate from Codeable yet. I'll probably do that shortly.

#2510137

Nope, the option "Send notification to a WordPress user with an ID coming from a generic field in the form" can only send the email notification to only one user.

#2510175

I'm confused now. I thought the code you supplied would send to all users with the Board Member role.

Do you mean I have to edit elsewhere in the custom code and not in the shortcode? We have 13 users that will have the Board Member role that should receive these notifications, but I still have to create the user accounts for 5 of them. We have 8 on there right now.

But for the purpose of testing right now, we'd rather use the administrator role temporarily. There are 3 user accounts with that role.
What code do we need to change in what you provided to do this?

#2510201

You are referring wrong ticket, the ticket you mentioned above can only send to one adminstrator user:
https://toolset.com/forums/topic/send-email-notification-to-user/

In this case, you can use filter hook "cred_notification_recipients" to add more recipient emails, I have modified the custom code snippet code "notifications-to-board-members" as below:

add_filter('cred_notification_recipients', function($recipients, $notification, $form_id, $post_id) {
 
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Slice Application Form Submission' == $notification['name'] ) {
      $role = 'administrator'; // here change role slug, for example: board_member
      $blogusers = get_users( 'orderby=user_id&role='.$role);
      foreach ( $blogusers as $user ) {
          $recipients[] = array(
            'to'        =>  'bcc',
            'address'   => $user->user_email,
            'name'      =>  $user->display_name,
          );
      }
    }
          
    return $recipients;
}, 10, 4);

Please test again, check if it is what you want.

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

#2510283

I think the confusion stems from two different replies from Minesh in the ticket I linked. The first bit of code he provided only allowed for one user, but then if you scroll down further into that ticket, he adjusted it to pull all users of a specific role, which is what the phillipM author said he ideally wanted. That's what I was hoping to do here, but I just couldn't figure out where to put the first code section for the field from Minesh's reply.

I just tested what you have set up and I'm having strange results. On my first test, the notification arrived as expected and I was happy to see that. But then I noticed that the URL for the page was not a clickable link in the e-mail notification. So I went into the notification and changed this:

Full details on this applicant and Board voting form can be found at :
[wpv-post-url]

To this:

Full details on this applicant and Board voting form can be found at :
<a href="[wpv-post-url]">[wpv-post-url]</a>

After I did that, I tested again and the notification did not send. So I installed the Post SMTP plugin so I could view its e-mail log. I configured Post SMTP and did a couple of successful test e-mails that I did receive at the test address.

I then submitted the form again to see if the notification would go through and it did not. So I changed the [wpv-post-url] bit back to how I had it before thinking it would work again. But it didn't. I then deactivated Post SMTP thinking maybe it had something to do with what was happening. That didn't work either. I tried using the "Send a test e-mail button" that you have under the notification and that worked fine. So now I have no idea why I can't get the notification to send. It did send fine on my first test and the only thing I tried to change was to make the URL a clickable link in the e-mail.

I'm thinking what you provided for sending to the administrator role users worked properly that first time because I received it at an e-mail for one of the admin users. But now I can't get it to work again when sending test form submissions. I can only get it to send from the "Send a test e-mail" button in the area to edit the Notification. I've reactivated Post SMTP so you can see any e-mail log entries.

If you would be able to test it on your end, you can do so here:

hidden link

On this form, required fields should be First Name, Last Name, E-Mail Address, State, Board Member, and Freelancer Image. You should be able to leave all the other fields blank, but I've been using "Test Freelancer ##" as the Freelancer Title field. For the Freelancer Image field, we're trying to set that up to be an image field that must be 585 x 585 and I'm getting help with that in another Toolset ticket currently. It's nearly perfect now as the only remaining problem seems to be with the error text that appears if an off -size image is uploaded. But you'll want to use this image:

hidden link

That one is sized properly, so just use that one on any test form submissions you do. I'll be deleting all tests and this image when testing is done. Thanks for your patience and willingness to help with this and I'm sorry if I did something wrong to cause it to stop working. I thought it would be an easy fix to make that URL for the page a clickable link. I don't know what I've done wrong here.

#2510873

Thanks for the details, I have done below modifications in your website:
1) Edit the post form:
hidden link
in section "Slice Application Form Submission"
enable option: Send notification to an email specified in an email field included in the form:
And choose the email field "E-Mail Address",
The email "To" option is required to send an email, the custom PHP codes only add "BCC" email addreses, so you need to setup at lease one email in option "To"

2) In section "Notification e-mail body", change the shortcode to:

<a href="[wpv-post-url]">[wpv-post-url]</a>

Please test again, check if it is fixed. thanks

#2511115

All good now! Hmmm, I did look at the recipients now that you mention it. Maybe I did change that and forgot that I had done so. That would make sense as to why it was no longer sending. Sorry about my forgetfulness if that's the case. I've run a couple tests and it is working.

Thanks hugely for the assistance again. Marking this one resolved. Now on to the next piece to the puzzle on this complex system.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.