Form Pre-submission Filter broken with PHP 8.0

mediaryte
Fri, 17 Feb 2023 22:08:16 +0000
Get Help

We’ve been using the following script successfully for quite sometime, but with the change to PHP 8.0, we are running into an issue without an error to identify a fix.

On submission, the form notification is supposed to send to the selected contact’s email address, which is contained in custom metadata from a custom post type “Contact”

With the update to PHP 8.0, the notification is still sending, but to the default notification recipient provided in the notification settings and no longer sent to the selected contact.

Error logs do not reveal what is broken, nor have I been able to find another post about this issue. Any insight is greatly appreciated!

// Dynamic Contact Form: Send To Selection
add_filter("gform_pre_submission_filter_1", "add_to");

function add_to($form){

	global $post;

	// Pull post->ID to match post->ID and fill with custom field 'email'.

	$post_id = $_POST["input_1"]; // gets input value

	$args = array(
		'post_type' => 'contact',
		'p' => $post_id,
		'posts_per_page' => 1
	);

	$wp_query = new WP_Query( $args );

	if( $wp_query->have_posts() ) {
		while( $wp_query->have_posts() ) : $wp_query->the_post();

		$to = get_post_meta( $post->ID, '_x_contact_email', true ); // gets contact email address

		$form["notification"]["to"] = $to; //setting notification TO field to the list of fields

		endwhile;
	}

	return $form; //returning modified form object
}

4 posts - 3 participants

Read full topic