Add a Custom Metabox and Input Fields to Your Event Registration Pages
Part 2 of 11 in the Build an add-on series. Previous: Admin page. Next: Save event data.
With a list of styles in place, the event editor needs a way to pick them. This part adds a Styles box with one checkbox per style to the event editor in wp-admin, and the same fields to the front-end event form.
If all you need is one or two extra text or select fields on events, event attributes add them from the settings page with no code.
What you will build
A Styles meta box in the sidebar of the event editor, and a Styles section on the form printed by the [event_form] shortcode.
Complete part 1 and add at least one style.
Steps
-
Add these two lines inside
my_em_styles_init():add_action( 'add_meta_boxes', 'my_em_styles_meta_boxes' );add_action( 'em_front_event_form_footer', 'my_em_styles_frontend_form_input' ); -
Add the functions below to the end of the file.
-
Open any event for editing. The Styles box is in the side column.
function my_em_styles_meta_boxes(): void {
add_meta_box( 'em-event-styles', __( 'Styles', 'my-em-styles' ), 'my_em_styles_fields', array( EM_POST_TYPE_EVENT, 'event-recurring' ), 'side', 'low' );
}
function my_em_styles_fields(): void {
global $EM_Event;
$selected = $EM_Event instanceof EM_Event && is_array( $EM_Event->styles ) ? $EM_Event->styles : array();
wp_nonce_field( 'my_em_styles_event', 'my_em_styles_nonce' );
foreach ( my_em_styles_get() as $style_id => $style_name ) {
printf(
'<label><input type="checkbox" name="event_styles[]" value="%d" %s> %s</label><br>',
absint( $style_id ),
checked( in_array( (int) $style_id, $selected, true ), true, false ),
esc_html( $style_name )
);
}
}
function my_em_styles_frontend_form_input(): void {
if ( ! my_em_styles_get() ) {
return;
}
?>
<section class="event-form-styles">
<h3><?php esc_html_e( 'Styles', 'my-em-styles' ); ?></h3>
<div class="input"><?php my_em_styles_fields(); ?></div>
</section>
<?php
}
How it works
The meta box. Events are a WordPress post type, so add_meta_box() works as it does for posts. The box is registered for two post types: event, and event-recurring, which is the post type of repeating events. Recurring events made in the normal editor are event posts and need nothing extra. If your site defines event archetypes, add their post type names to the array.
The fields. my_em_styles_fields() prints the checkboxes, and both the meta box and the front-end form call it, so the field names are the same in both places. The global $EM_Event is the event being edited. Its styles property doesn't exist yet. Part 4 adds it, and until then the function treats it as an empty list, so no box is ticked.
The nonce. wp_nonce_field( 'my_em_styles_event', 'my_em_styles_nonce' ) adds a hidden field that proves the request came from a form containing these checkboxes. Part 3 depends on it, and not only for security: it is how the save code tells "no styles ticked" apart from "this request has nothing to do with styles".
The front-end form. The em_front_event_form_footer action fires in the event submission form, after the bookings section and before the submit button. The wrapper uses the same section, h3 and div.input structure as the sections around it, so it picks up the form's styling.
Test it
- Edit an event in wp-admin. The Styles box lists the styles you added in part 1.
- Add a page containing
[event_form], view it while logged in as someone who can submit events, and check that a Styles section appears above the submit button. - View the page source and search for
my_em_styles_nonce. The hidden field is there.
Ticking a box and saving does nothing yet. That is the next part.