Form Pre-submission Filter broken with PHP 8.0

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
}

On further inspection with some help, we did find an error:

Undefined array key "input_1"

From the script, the following needs to be updated/replaced - I’m not sure what the new value for the array key would be.

$post_id = $_POST["input_1"];

You would replace 1 with the ID of the new field containing the post ID value. To prevent errors in the future, I recommend using the rgpost helper function to access the value:

$post_id = rgpost( 'input_1' );

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.