Skip Navigation

[Gelöst] Send CRED notification to user in select custom field

This support ticket is created vor 3 Jahre, 7 Monate. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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: Africa/Casablanca (GMT+00:00)

This topic contains 19 Antworten, has 2 Stimmen.

Last updated by larryL vor 3 Jahre, 6 Monate.

Assisted by: Jamal.

Author
Artikel
#1773881

I have a post form with a custom select field that pulls users from the wordpress user table. I'd like to send an email notification to the user selected. Is this possible?

#1774055

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

To be able to send a notification to the selected user, you will have to build the select field to hold the user's email on the value. Then you can configure the notification to be sent to the email selected in that custom field.

<select>
<option value="user@example.com">User 1</option>
  <option value="john@domain.com">Jhon</option>
</select>

I hope this makes sense. Let me know if you have further questions.

#1776615

That makes sense, it's just not possible. Users get added dynamically so there should be a way to pull their email address from the wordpress user profile and email it.

#1777027

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Can you elaborate more on how the list of users is pulled?
Can you allow me temporary access to check this form closely?
Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **.

#1779055

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

You are right, you can update the code to get the email of the user. Change this code:

            $options[] = array(
                '#value' => $user->ID,
                '#title' => $user->display_name,
            );

To:

            $options[] = array(
                '#value' => $user->user_email,
                '#title' => $user->display_name,
            );

But, this will save the user email instead of the ID in the "user id" field. Maybe that's not what you want.

I'll suggest implementing a custom code that will pull the user email from the ID and add it to the notification recipients, using the cred_notification_recipients hook.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

You can then scope this function to, only, this form(ID: 8755), check this pseudo code:

add_filter('cred_notification_recipients', 'add_recipients', 10, 4);
function add_recipients($recipients, $notification, $form_id, $post_id) {
  if ( $form_id == 8755 ) {
     // get the user ID from $post_id or from $_POST
     // get the user email by ID
     // modifiy the $recipients array to include the user email
    // return $recipients;
  }

  return $recipients;
}

I hope this helps. Let me know if you have any questions.

#1779681

Jamal, How do I target this in the notifications settings in the form? IOW, How do I tell the form to only send a specific notification to the selected user? Also, this function seems to break something. I'm not sure what yet, I'm still debuging. Thanks!

#1780073

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

We might have a simpler solution. Check this screenshot hidden link, the notification can take a user ID from a generic field in the form. So, I suggest the following:
- Add a "single line" generic field to your form.
- Hide this generic field with custom CSS.
- Add a Javascript snippet to the form that will sync the value of the selected user to the generic field.

Does it make sense? Let me know if you would need help with it.

#1782891

That field is not available there. Did you try it?

#1785709

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Do you mean the "user id" field? It won't be available on that list, as it is not a generic field.

You will have to add a generic field from the right sidebar, check this screenshot hidden link
Then with custom Javascript code, you can sync the value of the selected user to it, something like:

jQuery('select[name="wpcf-user-id"]').on('change', function(){
  var select = jQuery(this);
  jQuery('input[name="my-notification-field"]').val( select.val() );
})

Assuming that you named the generic field "my-notification-field".

I wanted to implement this for you, but I was afraid to break the styling of the form when switching to expert mode(in order to hide the generic field).
If you found trouble implementing this solution, please create a minimal form, with only the "user id" field and add it to a page, then create a notification for it and let me do it for you. Then you can see how I managed to add the generic field, to hide it, and the custom Javascript code to pull its value.

#1788393

So I'm clear. I would add a generic field (do I need to add an additional custom field as well?) and then add the script to the form. Can I just add this as an HTML block to the current form?

#1789355

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I am not sure to understand your question about adding an HTML block? Is it for the generic field or for the Javascript code?

In both cases, no, you don't need to add and HTML block.
For the Javascript code, you can add it to the Javascript section in the form editor.
And you must add a generic field from the sidebar instead of custom HTML.

There is no need to create another field. Generic fields are essentially useful, for when you don't need to create a custom field or save the information to the database.

I'll remain at your disposal.

#1795031

Alright, so I added the script and the field. This field would be hidden, no? What was your idea of what should happen next?

#1795251

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Yes, the field should be hidden, so, the user won't enter anything on it, because we will rely on the Javascript code to enter the user_id of on it when the user selects it in the existing "user id" field.
You added the field with the following code, right?

	<div class="row">
		<div class="col-sm-12">
			<label>my-notification-field</label>
			[cred_generic_field type='textfield' field='my-notification-field']
{
"required":0,
"default":""
}
[/cred_generic_field]
		</div>
	</div>

I'll suggest that you make it hidden by updating the first line( style="display:none;"):

	<div class="row" style="display:none;">

The, you need to give the field a name, that will be used by the custom Javascript. Update your shortcode to use the field attribute:

			[cred_generic_field type='textfield' field="my-notification-field"]

Then, configure the email notification to use this generic field to get the user_id of the user who should receive the notification. Check this screenshot hidden link

#1796519
Capture2.PNG

There are no options in that dropdown. Even if I click the refresh link. See attached.

#1797687

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I can see this issue. It turns to be a bug, which I have escalated to our developers.

As a workaround, don't configure a recipient on the notification. Then add the following code that will add the user's email to the notification's recipients:

add_filter('cred_notification_recipients', 'add_recipients', 10, 4);
function add_recipients($recipients, $notification, $form_id, $post_id) {
  if ( $form_id == 8755 ) {
    // get the user ID from $post_id or from $_POST
    $user_id = $_POST['wpcf-user-id'];
    // get the user email by ID
    $user = get_userdata( $user_id );
    $email = $user->user_email;
     // modifiy the $recipients array to include the user email
    $recipients[] = array (
        'to' => 'to',
        'address' => $email,
        'name' => '',
        'lastname' => ''
    );
  }
  return $recipients;
}

Let me know if that works for you.

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