Creating Custom Permalinks in WordPress

This tutorial forms (the final) part of our series of tutorials for creating an custom add-on for Events Manager called “Styles”, where we will be able to select from a list of styles during event registration. We are in the process of creating custom styles pages, and now need to register permalinks as part of this step.

This tutorial sort of doubles as a WordPress tutorial (although I found the codex tutorial most helpful when creating permalinks in Events Manager) as this principle could be adapted to work for any plugin. This doesn’t use any plugin-specific functions whatsoever.

Creating the rewrite rules

Firstly, we’ll have to add some rewrite rules to the array. We’ll hook into the rewrite_rules_array filter and add our own rules to the $rules array. For the purposes of this example, we’ll assume our events page is located at our imaginary site mysite.com/events/ What we want to acheive is the following:

  • mysite.com/events/styles/ – displays the list of styles and upcoming events
  • mysite.com/events/style/id#/ – displays the single style name with this id and its upcoming events
function my_em_styles_rewrite_rules_array($rules){
	$events_page_id = get_option ( 'dbem_events_page' );
	$events_slug = urldecode(preg_replace('/\/$/', '', str_replace( trailingslashit(home_url()), '', get_permalink($events_page_id)) ));
	$events_slug = ( !empty($events_slug) ) ? trailingslashit($events_slug) : $events_slug;
	$my_em_rules = array();
	if( !empty($events_slug) ){
		$my_em_rules[$events_slug.'styles$'] = 'index.php?pagename='.$events_slug.'&styles_page=1'; //styles list
		$my_em_rules[$events_slug.'style/(\d+)$'] = 'index.php?pagename='.$events_slug.'&style_id=$matches[1]'; //single style
	}
	return $my_em_rules + $rules;
}
add_filter('rewrite_rules_array','my_em_styles_rewrite_rules_array');

The first six lines involve retrieving the url of the events page and deciding to add our rules only if there is a valid events page. Lines 7 and 8 are the redirection rules, which use regex to identify when we access style pages.

$my_em_rules[$events_slug.'styles$'] = 'index.php?pagename='.$events_slug.'&styles_page=1'; //styles list
$my_em_rules[$events_slug.'style/(\d+)$'] = 'index.php?pagename='.$events_slug.'&style_id=$matches[1]'; //single style

The first line will only catch our styles list page. Since we don’t need to obtain any information from here, we’ll just add an extra querystring variable called styles_page and set it to 1.

The second line handles requests for specific style pages. The (\d+) bit will catch the id number and when we pass $matches[1] into the style_id querystring variable it will be replaced by the actual number in the requested url. If this stuff confuses you, you should read up more on regex and the preg_match php function

Register your querystring variables

That was the hard part… the rest is a piece of cake! Now that we’ve added our rewrite rules, we also need to register the new GET parameters we just created, as they will not be made available to the $wp_query object and we won’t be able to load our content properly without that.

add_filter('query_vars','my_em_styles_query_vars');
function my_em_styles_query_vars($vars){
	array_push($vars, 'style_id','styles_page');
    return $vars;
}

All we’re doing here is pushing our variable names into the $vars array, and passing it back onto the query_vars filter.

Flush the rewrite rules

Once this is all ready, you’ll want to flush the rewrite rules so that these new rules take effect. You can do this by visiting your Permalinks settings, each time that page is loaded, rules are refreshed.

If you want to initiate this with PHP, you’d use the following, but bear in mind you’d want to use it sparingly, such as when your custom add-on is activated for the first time:

global $wp_rewrite;
$wp_rewrite->flush_rules();