Skip to main content

Creating Custom Permalinks in WordPress

Build an add-on

Part 11 of 11 in the Build an add-on series. Previous: Custom event pages. This is the last part.

The style pages from part 10 work, but their addresses carry query strings. This last part maps /events/styles/ and /events/style/1/ onto them with rewrite rules.

What you will build​

AddressIs handled as
/events/styles/The events page with styles_page=1
/events/style/1/The events page with style_id=1

events stands for whatever the address of your events page is.

Before you start

Complete part 10, including the query_vars filter. Your site must use pretty permalinks (Settings then Permalinks set to anything but Plain).

Steps​

  1. Add these lines at file level, outside my_em_styles_init():

    add_filter( 'em_rewrite_rules_array_events', 'my_em_styles_rewrite_rules', 10, 2 );
    register_activation_hook( __FILE__, 'my_em_styles_flush_rules' );
    register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
  2. Add the functions below to the end of the file. my_em_styles_query_vars() is already there from part 10.

    Part 11: rewrite rules
    function my_em_styles_rewrite_rules( $rules, $events_slug ) {
    $page = trim( $events_slug, '/' );
    if ( $page !== '' ) {
    $rules[ $page . '/styles/?$' ] = 'index.php?pagename=' . $page . '&styles_page=1';
    $rules[ $page . '/style/(\d+)/?$' ] = 'index.php?pagename=' . $page . '&style_id=$matches[1]';
    }
    return $rules;
    }

    function my_em_styles_flush_rules(): void {
    flush_rewrite_rules();
    }
  3. Go to Settings then Permalinks and click Save Changes. This rebuilds the rewrite rules so that yours are included. Deactivating and reactivating the plugin does the same through the activation hook.

How it works​

WordPress turns a requested path into query variables by testing it against a stored list of regular expressions. Events Manager adds its own rules to that list for addresses that live under the events page, and passes them through em_rewrite_rules_array_events along with the events page's path. Adding yours there keeps them next to the rules they resemble and gives you the correct path without working it out from the page ID.

Each rule maps a pattern to a query string. pagename loads the events page, and the extra variable is the one part 10 reads. In the second rule, (\d+) captures the style ID and $matches[1] inserts it.

If the events page is the site's front page its path is empty, and the function adds nothing, because /styles/ at the top level of a site is too likely to clash with a real page.

Rewrite rules are stored in the database and only rebuilt on request. That is why step 3 is needed, and why the plugin flushes them on activation and deactivation. Never call flush_rewrite_rules() on every page load: it is slow and it writes to the database each time.

Test it​

  1. Visit /events/styles/, using your own events page address. The styles index from part 10 appears.
  2. Visit /events/style/1/. The page for style 1 appears, with the style's name as its title.
  3. If either shows a 404 page, save Settings then Permalinks again, and check that a real page or event doesn't already use that address.

Where to go from here​

The add-on is complete. The full file is on the overview page. Things a production version would add:

  • Print links to the style pages, for example from a #_STYLELINKS placeholder built on part 7.
  • Store the style list in its own table, or replace the whole add-on with a custom taxonomy as described in Extra taxonomies.
  • Remove an event's style rows when a style is deleted, and delete the option and rows in an uninstall.php file.
  • Make the strings translatable by loading a text domain.

The same hooks exist for locations and bookings under matching names (em_location_save, em_location, em_location_output_placeholder, em_booking_save and so on), listed in Hooks and filters.