Creating Your Own WordPress Permalink Structure For Custom Content

The latest feature I have added to WPVote is a page that shows you all pages submitted to the site for a specific domain. Below I will show how I created the custom permalink structure without creating custom pages.

The process took me a while to work out but it ended up being quite simple code. I wanted to have the urls in the format ‘http://wpvote.com/domain/www.domain.com/’. I didn’t want to have to create custom pages, it was a feature that should ‘just work’ – so I created some custom WordPress rewrite rules.

You can check out this this page for Binary Moon to see what I ended up with.

The code required 3 PHP functions which I added to the themes functions.php file but the first thing I had to do was initialize a new WordPress query var which would be used later on. I added this to a function that is called on the init action, but it could just be added to the top of the functions.php file.

global $wp;
$wp->add_query_var('bmDomain');

bm_parseQuery – Setup Query for The Loop

The first function hooks in when the WordPress query is being generated and, if the newly added variable is set, resets the page properties and adds a hook to load the template that will be used on the new path.

function bm_parseQuery() {
	global $wp_query;
	if (get_query_var('bmDomain') != '') {
		$wp_query->is_single = false;
		$wp_query->is_page = false;
		$wp_query->is_archive = false;
		$wp_query->is_search = false;
		$wp_query->is_home = false;

		add_action('template_redirect', 'bm_wpvoteTemplate');
	}
}

add_filter('parse_query','bm_parseQuery');

bm_wpvoteTemplate – Execute the Template

The template function simply loads the relevant template file. I am using a custom template just for this page so that I can display just the posts for the specific website.

function bm_wpvoteTemplate() {
	global $template;
	if (get_query_var('bmDomain') != '') {
		$template = get_query_template('domain');
		include ($template);
		exit();
	}
}

bm_rewrite – The Rewrite Rules themselves

Adding the rewrite rules is relatively straight forward. It uses a simple regular expression to convert a url pattern into a website query string. My knowledge of regular expressions is quite limited but if I display all the existing rules (print_r($rules)) then it’s easy to work out what the new query should be.

In the case of my WPVote page, it converts the query from a pretty permalink to a traditional query string (which is exactly what happens to all the pages).

In the example I game above it would change this: domain/www.binarymoon.co.uk/ into this: index.php?bmDomain=www.binarymoon.co.uk

I also added a second rule that would allow for domains with more than 1 page worth of post submissions.

One thing to note is that the new rules should be added to the start of the list. If you add the rule to the end then it won’t be reached as there are other rules that will catch it before you get there.

function bm_rewrite ($rules) {
	// add the rules
	$rules[]["domain/(.+?)/page/?([0-9]{1,})/?$"] = "index.php?bmDomain=\$matches[1]&paged=\$matches[2]";
	$rules[]["domain/(.+?)/?$"] = "index.php?bmDomain=\$matches[1]";
	return $rules;
}
add_filter( 'rewrite_rules_array', 'bm_rewrite' );

What Else?

The code above is quite specific to WPVote but I am sure there are areas that something like this could be used. It’s most useful for sites that do things that aren’t traditionally blog focused (since you could just use pages to do stuff like this).

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

30 Jun 2007

WordPress tips and tricks – Custom Page Templates

Some time ago I posted the first of my tips and tricks for WordPress, and I thought it was about time I posted some more so, to start things off, here is a short tutorial on custom page templates in...
11 Apr 2011

Optimizing WordPress 404’s

One of the great things about WordPress is how 404 error pages are handled. If a page isn’t found then you can show a proper dynamic error page giving the user things to do – this removes a lot of...
31 May 2007

WordPress tips and tricks – functions.php

Functions.php is a little known wordpress template file. Not many themes take advantage of it but , used properly, it can be incredibly powerful. The file can be used as a way to add your own functions to wordpress themes...
17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
17 Feb 2024

Creating a Custom HTML Elements: A Colour Picker

I recently needed to create my first custom element - a custom color picker. I wanted to improve the color picker feature on Brush Ninja however I couldn’t find an existing solution that met my needs so I decided to...