Request this workshop

I’m working on a DLINQ website and I’m recycling1 ideas I liked from the old ALT Lab website days.2 This one is about having a list of workshops that we normally offer and making it easy for people to request them. My goal is to try to meet people’s needs for workshops and make what we do easily accessible. At the same time I want to try to avoid offering workshops that no one comes to. That’s always depressing. It makes sense to have some off the shelf stuff and to offer it if you can get 6 of your friends interested or whatever bar we set.

The workshop page itself also becomes a nice resource for internal and external use. We’ve got a standard summary, useful resources, etc. that we can use when creating an event and a single place to update all that stuff. I will also return to this old post where I made it so I could copy the workshop post type into the Events Calendar Pro. A nice workflow for us.

But this post is about the request a workshop pattern. It’s dependent on Gravity Forms and ACF. I push back and forth between the two in ways that save me time and increase consistency.

Make a drop down list of Gravity Forms in ACF

I’ve got two different forms I want to let people change in the site. Previously, I’d have just hardcoded them in and altered the theme when I needed to. I created an options page in ACF. This make it so that we can change the Gravity Form associated with various things in one place and avoid having it hardcoded. That also makes it easier to move from dev to production. I don’t have to worry about messing up the Gravity Form IDs which might vary between the two installations.

You’ll note that I’m using the Gravity Forms list in three different form fields. Same function, multiple ACF fields. They use acf/load_field/name to pick the right ACF form fields to populate. You can also

//add gravity forms to acf field for the daily create challenge option
/**
 * Populate ACF select field options with Gravity Forms forms
 */

//might need something like https://wordpress.org/plugins/categories-for-gravity-forms/
function acf_populate_gf_forms_ids( $field ) {
	if ( class_exists( 'GFFormsModel' ) ) {
		$choices = [''];

		foreach ( GFAPI::get_forms( true, false, 'title', 'ASC' ) as $form ) {
			$choices[ $form['id'] ] = $form['title'];
		}

		$field['choices'] = $choices;
	}

	return $field;
}
add_filter( 'acf/load_field/name=form_id', 'acf_populate_gf_forms_ids' );
add_filter( 'acf/load_field/name=contact_gravity_form', 'acf_populate_gf_forms_ids' );
add_filter( 'acf/load_field/name=workshop_registration_form', 'acf_populate_gf_forms_ids' );

Make a drop down list of custom post types in Gravity Forms

This has been something I’ve done a number of times. It’s really useful. The list updates automatically as you publish more posts of a particular type.

I’m adding a trick or two here to make things more flexible.

You’ll note in the initial add_filter portion that I’m fetching a variable using get_field(‘workshop_registration_form’, ‘option’). That references the Gravity Form ID that we set up in the step above on the options page.

//add workshops dropdown to workshop form
$gf_workshop_request_id = get_field('workshop_registration_form', 'option');
$pre_render = 'gform_pre_render_' . $gf_workshop_request_id;
add_filter( 'gform_pre_render_' . $gf_workshop_request_id , 'populate_cpt_titles' );
add_filter( 'gform_pre_validation_' . $gf_workshop_request_id,'populate_cpt_titles' );
add_filter( 'gform_pre_submission_filter_' . $gf_workshop_request_id, 'populate_cpt_titles' );
add_filter( 'gform_admin_pre_render_' . $gf_workshop_request_id, 'populate_cpt_titles' );
function populate_cpt_titles( $form ) {
	

	foreach ( $form['fields'] as &$field ) {
		if ( $field->id != 7 ) { //only do this for field ID
	    		continue;
		}

		$field->placeholder = 'Select a workshop';

		$args = [
			'posts_per_page'   => -1,//get all of them
			'order'            => 'ASC',
			'orderby'          => 'post_title',//sort by name
			'post_type'        => 'workshop', // Change this to your Custom Post Type
			'post_status'      => 'publish',//only published
		];
		$custom_posts = get_posts( $args );

		$options = [];
		foreach( $custom_posts as $custom_post ) {
			$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];//you could return other things here like the post id if that was more useful
		}

		$field->choices = $options;
	}

	return $form;
}

Auto-populate the workshop selection in Gravity Forms

Now if you’re on a workshop page and you want to request that workshop, why not make it automatically chosen? It’s a little thing, but I think good experiences are made of little things. In this case we’re doing two things. First, we check the box in Gravity Forms for the field to allow it to be auto-populated and we give the variable a name (workshop in this case, but it could be anything).

Since I’m building it into the theme, I use the gravity_form function. The fifth variable is for field values and I want to set the workshop variable to the title of the page using WP’s get_the_title function.

   gravity_form( $gf_id, false, false, false, array('workshop' => get_the_title()), true, false, true ); 

1 Hopefully they’re good so I return to them rather than I’m just running out of ideas.

2 Sadly that URL doesn’t even exist anymore.

Leave a Reply