Skip Navigation

[Resolved] cred_mail_header not filtering per num_notification or formid

This support ticket is created 7 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 7 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#445572

I have entered this code on functions.php:

function add_my_header($headers, $formid, $postid, $num_notification) {
$myuser = get_user_by('ID', $postid);
$num_notification = 1;
$formid = 316;
$myheaders = array( 'To: '. $myuser->user_email );
return array_merge($headers, $myheaders);
}

I have two notifications with different content set up through a cred form in admin to go to a set email address.

I am attempting to develop a conditional based use case and the starting point is simply adding a second 'to' email to the second notification (num_notification 1). However, I'm finding that the second 'to' email address is adding to both notifications, and also if I change $formid to a different form, it still sends it from form 316.

Is the above code correct?

On a side note, while I'm creating a support ticket - is it possible to disable a notification through this hook? eg in functions.php have a conditional parameter that triggers disabling notification 1 for form 316 from sending entirely?

#445652

Nigel
Supporter

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

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

Hi Natasha

Checking the source code it seems that the last update to CRED which added notification names also altered the cred_mail_header filter, but the documentation has not been updated (I'm adding an internal ticket for that now).

There is an additional argument for the notification name, and the notification number is now the 5th argument, which is why your code will not work, because your $num_notification is actually the notification name.

Here is an updated template for using the cred_mail_header filter with 5 arguments which you can modify to add your code to:

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

	// your code here

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

As for your last question, I can't find any hook that would specifically allow you to do that.

The only way I could think of trying to do something like that would be to manipulate the form settings in the database for CRED notifications (they are stored in the wp_postmeta table with a key of _cred_notification against the form post) using the cred_before_save_data hook. I don't know if it would work, and you should certainly only try it if you feel comfortable recreating the settings object which is stored as a serialised string.

#445951

Thanks, I've used your code but now get the error (twice, as below, for some reason):

Warning: Missing argument 5 for add_my_header() in /home/codeflex/public_html/subs/fbi/wp-content/themes/Family-Business-Institute/functions.php on line 596

Warning: Missing argument 5 for add_my_header() in /home/codeflex/public_html/subs/fbi/wp-content/themes/Family-Business-Institute/functions.php on line 596

If I change the code to:

add_filter('cred_mail_header', 'add_my_header', 10, 4);
function add_my_header($headers, $formid, $postid, $notification_name, $notification_number=1) {
	$myuser = get_user_by('ID', $postid);
    if ( $formid === 316 ) {
			$myheaders = array( 'To: '. $myuser->user_email );
			return array_merge($headers, $myheaders);
		
	}
}

I no longer get the error, but the added 'to' email sends to both notifications, instead of just the second (1) so I dont think I have the format correct.

#446005

Nigel
Supporter

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

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

Hi Natasha

add_filter takes 4 arguments.

1. is the name of the filter you are hooking into
2. is the name of the function you are adding to it
3. is the priority level (default is 10, higher numbers run later if there are multiple filters attached)
4. is the number of arguments that will be passed to your function

There are 5 arguments available with the cred_mail_header filter, but you are only passing 4 of the arguments. You need to change 4 to 5 to pass all the arguments, including the notification number.

You can add a conditional check so that your code only runs for a particular notification number, e.g.

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

		// code just for notification 1
		$myheaders = array(); // add headers

		$headers = array_merge( $headers, $myheaders );

	} else {

		// optionally add code for any other notification
		// which similarly adjusts $headers

	}
 
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.