Home » Enhancing Events Manager Registration Form with Custom Fields

Enhancing Events Manager Registration Form with Custom Fields

by Shubham Verma
Published: Last Updated on 2 comments 1.6K views 4 minutes read

Introduction:

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.

Adding Custom Fields:

To integrate custom fields into the registration form, we utilize four hooks: two actions and two filters.

1. Action: Hooking the Form Template

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');

2. Filter: Saving Form Input

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');	

Displaying Custom Field in Admin:

To display the new custom field in the admin interface, two additional hooks are used.

3. Action: Admin Field Display

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);

4. Filter: Activating Custom Field in View

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.

You may also like

2 comments

Mishaelanimi March 14, 2024 - 11:37 pm

Hello.

Reply
Shubham Verma March 18, 2024 - 7:06 pm

HI @Mishaelanimi

Reply

Leave a Comment

ShubhamVermma.com: Your one-stop destination for live technical tutorials. Master PHP, Laravel, WordPress, Node.js, and more with our expert instructors.

Subscribe

Subscribe my Newsletter for new Articles. Let's stay updated!

A Technology Media Company – All Right Reserved.