Deleting Gravity Form Reservations

I’m messing around with a reservation system that meshes with the Events Calendar Pro calendar system we use. We’re not trying to get people to pay for anything and we have a few other simple things that we want to do that makes a lot of the paid plugins for reservations messy.

One of the things I wanted to make possible was for people to click on a link to delete their reservation for an event. We’re not talking nuclear secrets or money here so I think this will be reasonably safe.

Create a password type of thing

Let’s generate a random code using WP’s built in function wp_generate_password. I set this one to be 20 characters long and to have no fancy stuff that might mess up the URL.

//create code for deletion to put in gravity form
function dlinq_registration_deleter( $value, $lead, $field, $form){
	return wp_generate_password(20,false,false);
}

Write that password to the form

So now that I have that function, I can use the gform_save_field action to trigger it. By appending _5_11, I’m specifying that this only happens with the form with ID 5 and on the field with ID 11.

Not bad for a few lines of code.

add_action( 'gform_save_field_value_5_11', 'dlinq_registration_deleter', 10, 4 );

Send it in the email

Since Gravity Forms lets us send emails with form variables, I can create the link pretty easily.

The shortcode below gets us the URL from the event where the registration occurred and merges in the password/unique string we generated.

{embed_url}?delete={deletecode:11}

Look for deletion requests

Now we need our event pages to look for URL parameters in the URL that say ‘delete’ and have a code match in the Gravity Forms field for the password. isset is a handy PHP function that won’t freak out if the parameter isn’t set. Then we’ve got some code to search for a matching entry in Gravity Forms using GFAPI::get_entries. If it finds a match, it deletes it. It may be that I just flag it as deleted or something less drastic . . . but it works.

function dlinq_check_to_delete(){
	if( 'tribe_events' == get_post_type()){
		if(isset($_GET["delete"])){
			$passcode= $_GET["delete"];
			$search_criteria = array(
			    'status'        => 'active',
			    'field_filters' => array(
			        'mode' => 'any',
			        array(
			            'key'   => '11',
			            'value' => $passcode
			        )
			    )
			);
			$entry = GFAPI::get_entries(5, $search_criteria);
			if(sizeof($entry)>0){
				$entry_id = $entry[0]['id'];
				GFAPI::delete_entry( $entry_id );//maybe we don't want to delete this?
			}			
		}
	}
}
add_action('wp_head','dlinq_check_to_delete');

Leave a Reply