We recently built a course registration website on WordPress using two of our favourite plugins: Gravity Forms and Advanced Custom Fields. In this blog post, we will explain how we accomplished this and provide some tips to make it easier for you.
To keep things simple, we decided not to use The Events Calendar plugin, although it is great for most purposes. It lacks a visually appealing design and doesn’t seem to use the Gutenberg block editor for individual default events editing purposes. Implementing it would have required creating numerous custom templates, which was not feasible due to time and budget constraints.
Instead, here’s what we did:
- We created two parent categories: one for subjects and another for teachers. Then, we utilised the default WordPress post type to organize the courses for registration. These categories were used to filter the courses on the front end using the Category Ajax Filter plugin. They were also employed for entry management in Gravity Forms on the admin side, as well as for sending relevant information to the registrant regarding their chosen course.
- Gravity Forms handled all the course registrations. Instead of creating separate forms for each course, we designed a single form and used the post title to populate the Gravity Forms product name.
- For the time and dates of the events, we considered using actual date/time fields. However, since we didn’t require that level of complexity for this project, we opted for simple Advanced Custom Fields text boxes. The editor can easily copy and paste the date into the field. If desired, you can customize the display of this information in the post template, but given our time constraints, manual copying sufficed.
Now, let’s dive into the interesting part with Gravity Forms.
We needed the form to send the registrant an email notification containing the time and date of the course. To achieve this, we created functions that transfer these data values from Advanced Custom Fields to hidden fields in Gravity Forms.
As this is only one form, each time a Gravity Forms form is submitted, it will only send the Advanced Custom Fields data relating to that post to the registrant.
Here’s the code you can use in your functions.php file or custom plugin
// Send Gravity Forms the ACF fields Times and Dates
if ( !is_admin() ) {
add_filter( 'gform_field_value_coursedates', 'populate_coursedates' );
function populate_coursedates( $value ) {
global $post;
$coursedates = get_field( 'coursedates', $post->ID );
return $coursedates;
}
add_filter( 'gform_field_value_coursetimes', 'populate_coursetimes' );
function populate_coursetimes( $value ) {
global $post;
$coursetimes = get_field( 'coursetimes', $post->ID );
return $coursetimes;
}
add_filter( 'gform_pre_submission', 'populate_hidden_fields' );
function populate_hidden_fields( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 22 ) {
$_POST['input_22'] = apply_filters('gform_field_value_coursedates', '');
} elseif ( $field->id == 23 ) {
$_POST['input_23'] = apply_filters('gform_field_value_coursetimes', '');
}
}
}
}
// Get Child Cat for Courses
if ( !is_admin() ) {
add_action( 'gform_pre_submission', 'populate_hidden_field' );
function populate_hidden_field( $form ) {
// Get the current post ID
$post_id = get_the_ID();
// Get the parent category with ID 6
$parent_cat = get_category( 6 );
// Get the child categories of the parent category with ID 6
$child_cats = get_categories( array( 'parent' => $parent_cat->term_id ) );
// Get the first selected child category from the post
$first_selected_child_cat = null;
foreach ( $child_cats as $child_cat ) {
if ( has_category( $child_cat->term_id, $post_id ) ) {
$first_selected_child_cat = $child_cat;
break;
}
}
if ( $first_selected_child_cat ) {
// Set the value of the field with ID 24 to the name of the first selected child category
$_POST['input_24'] = $first_selected_child_cat->name;
}
}
}
// Get Child Cat for Teachers
if ( !is_admin() ) {
add_action( 'gform_pre_submission', 'populate_hidden_field_teachers' );
function populate_hidden_field_teachers( $form ) {
// Get the current post ID
$post_id = get_the_ID();
// Get the parent category with ID 1
$parent_cat = get_category( 1 );
// Get the child categories of the parent category with ID 1
$child_cats = get_categories( array( 'parent' => $parent_cat->term_id ) );
// Get the first selected child category from the post
$first_selected_child_cat = null;
foreach ( $child_cats as $child_cat ) {
if ( has_category( $child_cat->term_id, $post_id ) ) {
$first_selected_child_cat = $child_cat;
break;
}
}
if ( $first_selected_child_cat ) {
// Set the value of the field with ID 26 to the name of the first selected child category
$_POST['input_26'] = $first_selected_child_cat->name;
}
}
}