Skip Navigation

[Resolved] Send CRED notification to user ID

This support ticket is created 7 years, 3 months ago. 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 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+02:00)

Tagged: 

This topic contains 8 replies, has 3 voices.

Last updated by simon 7 years, 3 months ago.

Assisted by: Juan.

Author
Posts
#481310
screenshot-cred user id notification.png

I want to send a cred notification to the author of the parent post for my CRED form.

I've created a generic field on the form to return the author id for the parent - this works fine and I can see the correct user id there.

[cred_generic_field field="owner-id" type="textfield" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":0,
"generic_type":"user_id",
"default":"[wpv-post-author format='meta' meta='ID' id='accommodation']"
}
[/cred_generic_field]

However when I try and select the field in the notifications section of CRED, this field does not appear (no fields appear in the select).

What am I doing wrong?

How else could I achieve this? I obviously don't want the email address in plain text on the page even if it is hidden with CSS.

Thanks.

#481405

Nigel
Supporter

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

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

Hi Simon

You can't do this through the CRED settings, you will need to intercept the notification email and modify the header to add the required email address to the "To:" field, using the cred_mail_header hook: https://toolset.com/documentation/user-guides/cred-api/#cred-mail-header

You should be able to add something like the following to get this working (in your theme's functions.php file or via a plugin such as Code Snippets):

/**
 * Customise CRED notifications
 */
function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {

	if ( 'Custom notice' == $notification_name ) {
		$userid = $_POST['owner-id'];
		$user = get_userdata( $userid );

		$to_header = array( 'To: ' . $user->user_email );

		$headers = array_merge($headers, $to_header);
	}
 
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

You should just need to edit that for the name of the notification.

Also note that if there is already a To: recipient set this will add to it rather than replace it.

#481408

Hi Nigel, it looks like it should work as the option is there in notifications. I would have though that this would be a common use-case - I doubt many developers want to display email addresses on the front-end. Could you pass this through as an enhancement request/bug fix?

Out of interest, what is the user-id notification and when if ever would it normally work?

#481466

Nigel
Supporter

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

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

The notification to a user specified in a user_id form field is for use on a CRED User form where you can add a user id field rather than a CRED Post form where you cannot.

I'll re-assign this ticket and label as a feature request so our developers can assess whether to add it to our roadmap.

#481475

Many thanks as always.

#481545

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

HI there, SImon 🙂

I have good news for you, or somethign like good news.

Nigel was slightly wrong: the notification to a user specified in a user_id form field of course can be used on CRED Post form, and you can indeed set a "user_id" generic type for a field. I did not test this to the end, meaning I did not check whether this indeed works and you get the email in the desired email address, but for sure this generic user_id field can be selectable in the dropdown.

Otherwise, this would not be a feature request but a bug, since we should not offer the option to a WordPress user which ID comes from a form field in post forms, if that is only meant for user forms.

But it does work. The only liitation here is that the field must be prsistent, so the "persist" property must be set to 1 instead of 0. Once you make that field persistent, it becomes available as sorce of the ID of the user that you want to notify.

I understand that making this field persistent might not match your plans, I will ask why we have this requirement.

Hope it helps.

#481555

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Sorry for the spam 🙂

I got an answer from our CRED development team. As I suspected, the user_id fields must be either existing or persisten genric fields, for a reason. At the moment that the form notifications are sent, the form submitted data has already been processed. That means that we stored the permanent fields and discarded and cleared the non persistent. Because of that, non persistent fields can not affect notifications in any way.

If you do not want to keep this user_id field after sending the notification, I am sure that we have some action somewhere that lets you do a cleanup afte the fact.

Hope it helps.

Regards.

#481820

Hey Juan, thanks for the clarification. I've already implemented the php solution, but it is not ideal. I'll have another look at the generic field approach.

Best.

#481845

I still have one more question !

As I said, I've implemented the php solution that Nigel passed me. I'm now using a custom field for the email address - the following is a snippet from the implemented code:

        //
        // Get contact email for the accommodation
        $ownerEmail = get_post_meta($parentPostId, 'wpcf-acc-kontakt-email', true); 
        $to_header = array( 'To: ' . $ownerEmail );
        $headers = array_merge($headers, $to_header);

For the cred_mail_header action to be run, I need to create a valid notification (if I create the notification without a To email address, then the action is not triggered). So I have created a dummy noreply To address.

Ideally I would like to replace (ie. remove) this dummy address in the action, but I cannot see a way to do this.

It is not present in the $headers array passed to the action. I would expect to be able to modify existing addresses in the mail header - in fact $headers just contains:

(
    [0] => Content-Type: text/html
)

None of the existing address details seem to be available. Perhaps this too is a bug?

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