Skip Navigation

[Resolved] Dynamic "From" email and name fields in notifications

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.

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 4 replies, has 3 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#556728

Hello,
I need the "from" email and name fields in a notification to be populated by fields.
For instance, "email" would be [wpv-current-user info="email"] and name [wpv-current-user].
How may I do this ?
Thank you.

#556781

It is not possible in CRED.

What I can do is ask Luo, our CRED feature request master, to file this new feature as a request.

What you can try meanwhile is to use Custom Code with this CRED Hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header

There you can pass new Header Information as listed here:
hidden link

But only the "from" attribute is a modifiable one.

You will get the Current user Information with some basic WordPress API:
https://codex.wordpress.org/Function_Reference/wp_get_current_user
https://codex.wordpress.org/Function_Reference/get_userdata

That information can then be used to populate the CRED Hook.
There is a Code sample on the DOC page linked above.

I will assign this ticket to Luo now so that he can reply here on Monday.

#556784

Thank you.
I've looked into it and made this

/**************************************************
 * Headers FROM des Notifications
 **************************************************/
function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
    $myheaders = array();
    $currentuser = get_user_by(get_current_user_id());
    if ($formid==505 && $notification_number==0) {
    	$myheaders = array( 'From: '.$currentuser->user_email );
        return array_merge($headers, $myheaders);
    }
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

But it doesn't work... Can't understand what's the trouble in it ?!
Thank you !

#557075

Dear Roman,

Please try to modify your PHP codes as below and test again:

/**************************************************
 * Headers FROM des Notifications
 **************************************************/
function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if ($formid==505 && $notification_number==0) {
		
		$myheaders = array();
		$user_id = get_current_user_id();
		$currentuser = get_user_by('ID', $user_id);
        $myheaders = array( 'From: '.$currentuser->user_email );
        $headers = array_merge($headers, $myheaders);
    }
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

More help:
https://developer.wordpress.org/reference/functions/get_user_by/
Parameters:
$field
(string) (Required) The field to retrieve the user with. id | ID | slug | email | login.
$value
(int|string) (Required) A value for $field. A user ID, slug, email address, or login name.

#557094

Worked great ! Thank you very much.