Skip Navigation

[Resolved] Errors on code snippets provided previously.

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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by lesleeM 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2621567

Your support had provided us with a code snippet that got some needed functionality working for us. Then we had another issue requiring another code snippet, which you provided for us as well. But the problem was the support person that added the 2nd snippet put it together in the same snippet with the first one because they could both be described by the title of that snippet, but these were two separate bits of functionality. I left it as is at the time because it worked for both features and I don't argue with success.

But now, I was preparing to submit another ticket that was going to require a modified version of the first snippet to make it work for the new needed feature. I decided to separate these two snippets into two different ones to avoid confusion with that so I could just point to the first one in asking my next question. The second snippet in there was totally separate from what I intended to ask. But now when I save both of them, I'm getting an error message on both. The error on the first one says:

-----------------------------------------------

syntax error, unexpected end of file in -----/wp-content/toolset-customizations/notification-recipients.php on line 48

A problem occurred when executing snippet "notification-recipients". The result of include_once is: ""

-----------------------------------------------

The error on the second one says:

-----------------------------------------------

[2023-07-06 01:48:41, frontend] syntax error, unexpected '}', expecting end of file in -----/wp-content/toolset-customizations/send-registration-notification-to-ride-leader.php on line 50

A problem occurred when executing snippet "send-registration-notification-to-ride-leader". The result of include_once is: ""

-----------------------------------------------

I've replaced the paths on the errors with ----- for security reasons.

The first snippet was set up to pull the e-mail addresses from a custom field (E-Mail) of a custom content type (Registrations) to make those the recipient list for a post form submission (Notifications).

The second snippet was set up to pull the e-mail address from a custom field (E-Mail) of a related content type (Ride Leaders) to make it a recipient for a post form submission (Registrations).

I decided to separate those into two different snippets to avoid confusion on what I intended to ask in my next ticket. But now I've broken both these snippets. I tried putting them back together, but when I pasted the second one back in with the first one, I'm still inexplicably getting an error with that as well. I don't understand why that's the case. Please help because now this functionality for both is broken on the live site.

Sorry about this. I didn't expect separating these to cause these errors.

The first code snippet is titled notification-recipients . The second code snippet I've newly added as where I wanted to put the second snippet that was previously in notification-recipients is titled send-registration-notification-to-ride-leader.

The reason for the confusion here is we have a custom content type called Notifications. But we also have notifications that come from post forms. The first one is a list of e-mail addresses to receive Notifications (the custom content type). The second one is an e-mail address (or two) that should receive notifications from a post form. That's why your support person decided to put the second one in together with the first one under notification-recipients, not realizing there were two different versions notifications being handled and that the second didn't have any relationship to the first.

#2621591

Hi,

Thank you for contacting us and I'd be happy to assist.

An error like this can appear from the snippets if one or more of the closing brackets needed for the function are missing or there are some unexpected characters or empty spaces, appended at the end.

To fix this, I'll recommend deleting the existing code snippets from the Toolset's custom code section and adding them again. You'll just have to make sure to copy only the relevant code from the original support tickets, where they were shared.

In case the issue still persists, you're welcome to share the temporary admin login and the FTP access details, so that I can troubleshoot this in more detail.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2621979

Thank you for sharing these details.

1. Snippet - 'notification-recipients':

I've checked the code saved in this snippet and it is the same code as shared in Minesh's final comment in this thread:
https://toolset.com/forums/topic/post-form-e-mail-notification-recipients-question/#post-2608855

Tested the code on your and my website and it is not throwing any errors/warnings. So, I understand that you've reverted it back to its full original form.

2. Snippet - 'send-registration-notification-to-ride-leader'

This snippet is empty at the moment, so it must be the one where you tried to include the newly split function, that didn't work and resulted in the error.

3. Splitting of the code snippet:

Here is the basic structure of the existing complete code:


add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {

	// Check notification name matches target notification
	if ( $form_id == 2327 && isset($notification['name']) && 'Ride Notification' == $notification['name'] ) {
		........
	}

	if ( $form_id == 2083 && isset($notification['name']) && 'Ride Registration Notification' == $notification['name'] ) { 
		........
	}

	return $recipients;
}

The first 'if' part is executed for the form with ID '2327' and when dealing with its notification named 'Ride Notification'.

The second 'if' part is executed for the form with ID '2083' and when dealing with its notification named 'Ride Registration Notification'.

To split it into two separate code snippets, you can use the following steps:

1. The first part that you can replace in the original 'notification-recipients' snippet will look like:


add_filter('cred_notification_recipients', 'modify_recipients_2327', 10, 4);
function modify_recipients_2327($recipients, $notification, $form_id, $post_id) {

	// Check for specific form and notification name
	if ( $form_id == 2327 && isset($notification['name']) && 'Ride Notification' == $notification['name'] ) {
		$ride_id = toolset_get_related_post($post_id, 'ride-notification');
		$registration_posts = toolset_get_related_posts( 
			// get posts related to this one
			$ride_id, 
			// Relationship between the posts
			'ride-registration', 
			// Additional arguments
			[
				// Get posts where $writer is the parent in given relationship.
				// This is mandatory because we're passing just a single $writer post as the first parameter.
				'query_by_role' => 'parent', 
				'role_to_return' => 'child',
				'return' => 'post_id'
			]
		);
		foreach($registration_posts as $registration_id){
			$address = get_post_meta($registration_id, 'wpcf-email', true);
			$name = get_post_meta($registration_id, 'wpcf-name', true);
			if(!$address){
				continue;
			}
			// Add a BCC to registration email
			$recipients[] = array(
				'to'        =>  'bcc',
				'address'   => $address,
				'name'      =>  $name,
			);
		}
	}

	return $recipients;
}

The second part that you can place in the new 'send-registration-notification-to-ride-leader' snippet will look like:


add_filter('cred_notification_recipients', 'modify_recipients_2083', 10, 4);
function modify_recipients_2083($recipients, $notification, $form_id, $post_id) {

	// Check for specific form and notification name
	if ( $form_id == 2083 && isset($notification['name']) && 'Ride Registration Notification' == $notification['name'] ) { 
		// getting parent ride ID
		$ride_id = $_POST['_cred_cred_prefix_cred_container_id'];
		$ride_leader_posts = toolset_get_related_posts( 
			// get posts related to this one
			$ride_id, 
			// Relationship between the posts
			'ride-ride-leader', 
			// Additional arguments
			[
				// Get posts where $writer is the parent in given relationship.
				// This is mandatory because we're passing just a single $writer post as the first parameter.
				'query_by_role' => 'parent', 
				'role_to_return' => 'child',
				'return' => 'post_id'
			]
		);
		foreach($ride_leader_posts as $ride_leader_id){
			$address = get_post_meta($ride_leader_id, 'wpcf-ride-contact-email', true);
			if(!$address){
				continue;
			}
			// Add a BCC to registration email
			$recipients[] = array(
				'to'        =>  'to',
				'address'   => $address,
			);
		}
	}

	return $recipients;
}

I hope this helps and please let me know if you need further assistance.

#2621999

OK, I did that. It looks like the 2nd one is OK, but I still need to test the functionality. It isn't showing "Error" now.

But the 1st one is still showing "Error", so I've done something wrong again there.

When I say showing "Error" I'm meaning that on the Toolset | Settings | Custom Code area, it shows a red "Error" under the word Active in there.

#2622029

Oh wait. The "Error" is gone now. It made me resave it and now it looks OK.

#2622031

My issue is resolved now. Thank you!