Create a Custom Events Admin Page
Part 1 of 11 in the Build an add-on series. Start with the overview if you have not set up the plugin file yet. Next: Event meta box.
The add-on needs somewhere to manage its list of styles. This part adds a Styles page under the Events menu, with a form to add or rename a style and a table to delete them. Almost all of it is standard WordPress. The Events Manager parts are the menu slug, the capability, and the $EM_Notices object for messages.
What you will build
A page at Events then Styles that stores styles in the my_em_styles option.
You need the plugin file and helper functions from the overview.
Steps
-
Register the menu hook. Add this line inside
my_em_styles_init():add_action( 'admin_menu', 'my_em_styles_admin_menu', 20 );Priority 20 runs after Events Manager has added its own menu items, so Styles appears below them.
-
Add the functions below to the end of the file.
-
Reload wp-admin. Styles is now in the Events menu.
function my_em_styles_admin_menu(): void {
add_submenu_page( 'edit.php?post_type=' . EM_POST_TYPE_EVENT, 'Event Styles', 'Styles', 'edit_others_events', 'events-manager-styles', 'my_em_styles_admin_page' );
}
function my_em_styles_admin_url( array $args = array() ): string {
return add_query_arg( array_merge( array( 'post_type' => EM_POST_TYPE_EVENT, 'page' => 'events-manager-styles' ), $args ), admin_url( 'edit.php' ) );
}
function my_em_styles_admin_page(): void {
global $EM_Notices;
if ( ! current_user_can( 'edit_others_events' ) ) {
wp_die( esc_html__( 'You do not have permission to manage styles.', 'my-em-styles' ) );
}
$styles = my_em_styles_get();
$action = isset( $_POST['my_em_styles_action'] ) ? sanitize_key( $_POST['my_em_styles_action'] ) : '';
if ( $action === 'save' ) {
check_admin_referer( 'my_em_styles_save' );
$name = sanitize_text_field( wp_unslash( $_POST['style_name'] ?? '' ) );
$style_id = absint( $_POST['style_id'] ?? 0 );
if ( $name === '' ) {
$EM_Notices->add_error( esc_html__( 'Please enter a style name.', 'my-em-styles' ) );
} elseif ( $style_id && isset( $styles[ $style_id ] ) ) {
$styles[ $style_id ] = $name;
update_option( 'my_em_styles', $styles );
$EM_Notices->add_confirm( esc_html__( 'Style updated.', 'my-em-styles' ) );
} else {
$styles[ $styles ? max( array_keys( $styles ) ) + 1 : 1 ] = $name;
update_option( 'my_em_styles', $styles );
$EM_Notices->add_confirm( esc_html__( 'Style added.', 'my-em-styles' ) );
}
} elseif ( $action === 'delete' ) {
check_admin_referer( 'my_em_styles_delete' );
foreach ( array_map( 'absint', (array) ( $_POST['styles'] ?? array() ) ) as $style_id ) {
unset( $styles[ $style_id ] );
}
update_option( 'my_em_styles', $styles );
$EM_Notices->add_confirm( esc_html__( 'Styles deleted.', 'my-em-styles' ) );
}
$edit_id = absint( $_GET['style_id'] ?? 0 );
$editing = $edit_id && isset( $styles[ $edit_id ] );
?>
<div class="wrap">
<h1><?php esc_html_e( 'Event Styles', 'my-em-styles' ); ?></h1>
<?php echo $EM_Notices; ?>
<div id="col-container">
<div id="col-left"><div class="col-wrap"><div class="form-wrap">
<h2><?php echo $editing ? esc_html__( 'Edit style', 'my-em-styles' ) : esc_html__( 'Add style', 'my-em-styles' ); ?></h2>
<form method="post" action="<?php echo esc_url( my_em_styles_admin_url() ); ?>">
<input type="hidden" name="my_em_styles_action" value="save">
<?php wp_nonce_field( 'my_em_styles_save' ); ?>
<?php if ( $editing ) : ?>
<input type="hidden" name="style_id" value="<?php echo esc_attr( $edit_id ); ?>">
<?php endif; ?>
<div class="form-field form-required">
<label for="style-name"><?php esc_html_e( 'Style name', 'my-em-styles' ); ?></label>
<input id="style-name" name="style_name" type="text" value="<?php echo $editing ? esc_attr( $styles[ $edit_id ] ) : ''; ?>" required>
</div>
<?php submit_button( $editing ? __( 'Update style', 'my-em-styles' ) : __( 'Add style', 'my-em-styles' ) ); ?>
</form>
</div></div></div>
<div id="col-right"><div class="col-wrap">
<?php if ( $styles ) : ?>
<form method="post" action="<?php echo esc_url( my_em_styles_admin_url() ); ?>">
<input type="hidden" name="my_em_styles_action" value="delete">
<?php wp_nonce_field( 'my_em_styles_delete' ); ?>
<table class="wp-list-table widefat striped">
<thead><tr>
<td class="check-column"></td>
<th><?php esc_html_e( 'ID', 'my-em-styles' ); ?></th>
<th><?php esc_html_e( 'Name', 'my-em-styles' ); ?></th>
</tr></thead>
<tbody>
<?php foreach ( $styles as $style_id => $style_name ) : ?>
<tr>
<th class="check-column"><input type="checkbox" name="styles[]" value="<?php echo esc_attr( $style_id ); ?>"></th>
<td><?php echo esc_html( $style_id ); ?></td>
<td><a href="<?php echo esc_url( my_em_styles_admin_url( array( 'style_id' => $style_id ) ) ); ?>"><?php echo esc_html( $style_name ); ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php submit_button( __( 'Delete selected', 'my-em-styles' ), 'delete' ); ?>
</form>
<?php else : ?>
<p><?php esc_html_e( 'No styles yet. Add your first one with the form on the left.', 'my-em-styles' ); ?></p>
<?php endif; ?>
</div></div>
</div>
</div>
<?php
}
How it works
The menu. The Events menu is the list screen of the event post type, so its slug is edit.php?post_type=event. The constant EM_POST_TYPE_EVENT holds the post type name. Passing that slug as the parent to add_submenu_page() puts your page in the Events menu, and the page's address becomes edit.php?post_type=event&page=events-manager-styles, which is what my_em_styles_admin_url() builds.
The capability. The page asks for edit_others_events, one of the Events Manager capabilities, so the people who can edit every event can also manage styles. WordPress checks it before showing the menu item, and the page function checks it again before changing anything.
Saving. Both forms post back to the page. Each carries its own nonce from wp_nonce_field(), and check_admin_referer() stops the request if the nonce is missing or wrong. The style name goes through sanitize_text_field() on the way in, and every value is escaped with esc_html(), esc_attr() or esc_url() on the way out.
Messages. $EM_Notices is the global object Events Manager uses for feedback. add_confirm() and add_error() queue a message, and echo $EM_Notices; prints whatever is queued in the standard notice markup.
IDs. A new style gets the highest existing ID plus one. IDs are never reused within the list, which matters because events store the ID, not the name.
Test it
- Go to Events then Styles, type
Formaland click Add style. A "Style added." notice appears and the style is listed with ID 1. - Add
Casual. It gets ID 2. - Click Formal in the table, change the name and click Update style. The ID stays 1.
- Log in as a user who can't edit other people's events, for example a Subscriber. The Styles menu item is not shown.