Table of Contents
The Events Manager plugin, available on WordPress.org, offers a range of features to manage events. While the free version provides many functionalities, adding custom fields to the registration form directly from the admin is exclusive to the PRO version. However, you can achieve this customization using hooks and functions in your theme’s functions.php
file.
To integrate custom fields into the registration form, we utilize four hooks: two actions and two filters.
The first action is responsible for attaching the form template and introducing the new field. In this example, we’ll add a “Company Name” field.
<?php function sr_add_custom_event_fields(){ ?> <p> // Add your custom field HTML here <label for='user_company'> <?php esc_html_e('Company', 'textdomain'); ?> </label> <input type="text" name="user_company" id="user-company" class="input" value="<?php if (!empty($_REQUEST['user_company'])) echo esc_attr($_REQUEST['user_company']); ?>" /> </p> <?php } add_action('em_register_form', 'sr_add_custom_event_fields');
The second hook, a filter, ensures that the input value from the custom field is saved in the database.
function sr_save_custom_event_fields (){ global $EM_Booking ; // Get the custom field value from the form submission if( ! empty( $_REQUEST['user_company'] ) ){ $EM_Booking->booking_meta['registration']['user_company'] = wp_kses( $_REQUEST['user_company'], array() ); } } add_filter('em_booking_add','sr_save_custom_event_fields');
To display the new custom field in the admin interface, two additional hooks are used.
The third hook adds the new field to the list of available fields in the admin, enabling you to choose which fields to display in the listing view.
function sr_table_custom_event_fields($template, $EM_Bookings_Table){ $template['user_company'] = __('Company', 'textdomain'); return $template; } add_action('em_bookings_table_cols_template', 'sr_table_custom_event_fields',10,2);
The fourth filter adds the custom field to the table when it’s activated in the view. This filter is also used when exporting data.
function sr_display_col_custom_event_fields($val, $col, $EM_Booking, $EM_Bookings_Table, $csv){ if( $col == 'user_company' ){ // Add custom field to the list of columns $val = $EM_Booking->get_person()->user_company; } return $val; } add_filter('em_bookings_table_rows_col','sr_display_col_custom_event_fields', 10, 5);
Conclusion:
By strategically employing these hooks and functions in your theme’s functions.php
file, you can enhance the Events Manager plugin’s registration form with custom fields. While the free version may lack direct support for this feature, this method empowers you to tailor the registration form according to your needs.
Support and Assistance:
Should you encounter any issues during the implementation of these custom fields, please don’t hesitate to leave a comment on this post. We’re here to help you overcome any challenges and make the most out of your customized Events Manager registration form.
4 comments