| Switching from Windows to Linux |
Custom Rewrite Rules in WordPress
Posted on: 07 August 2021 by Andrew Perry
Posted in: [Technology]

I was trying for quite a while to get a custom rewrite rule to work on one of my WordPress sites but to no avail. I found quite a number of pages online explaining how to do it and yet they all seemed to lose me part way through. Eventually I got there largely with the help of the WordPress Codex itself. Here is a brief account of what I was trying to achieve and how I eventually did it.
I have published my family tree on my own personal web site. There is a page for each individual person on the tree, and the URL for such a page is:-
https://andperry.com/family-tree/person/?id=xxxx
where xxxx is a the identifier for the given person in the family tree database. The WordPress page had a PHP code insert identifying the person from the value of $_GET['id']. The aim was to create a rewrite rule so that the page could be referenced with a URL of the form:-
https://andperry.com/family-tree/persons/xxxx/
The following block of code is placed into the functions.php file of the active theme:-
function add_custom_rewrite_rules()
{
global $wpdb;
$row = $wpdb->get_row("SELECT * FROM wp_options WHERE option_name='rewrite_rules'");
$option_value = $row->option_value;
if ((strpos($option_value,'family-tree') === false) || (isset($_GET['force-rewrite-update'])))
{
add_rewrite_rule('family-tree/persons/([A-Za-z0-9_-]+)/?',
'index.php?pagename=family-tree/person&id=$matches[1]','top');
flush_rewrite_rules();
}
}
function add_custom_vars()
{
add_rewrite_tag('%id%','([A-Za-z0-9_-]+)');
}
add_action('init', 'add_custom_rewrite_rules');
add_filter('init', 'add_custom_vars');
First take a look at the add_custom_rewrite_rules function. The main part of its functionality is contained within the if statement. There is nothing to stop this functionality from being executed on every page load, but due to the potential processing overhead for the call to flush_rewrite_rules, this is not a good idea in terms of performance. One alternative is to run all the functions once and then comment out the call to add_action. This is OK but I found in practice that it is possible for the rewrite rules to be subsequently lost, for example by updating permalink settings via the WordPress dashboard. An automated method of detecting the need to create the rewrite rule seemed a good idea, hence the first condition in the if statement. The second condition (looking for the URL parameter force-rewrite-update) simply provides a method of manually updating the rules during development.
That's not quite it however. The PHP script within the page in question originally contained a line of code of the form:-
$id = $_GET['id'];
This no longer works once the URL has been rewritten. The call to add_rewrite_tag (see above) is used to register the given parameter variable name, but is not tied into the use of the global $_GET variable. Instead we have to say:-
$id = get_query_var('id');
It then works a treat!
| Switching from Windows to Linux |
