I have a Wordpress site I'm developing, which needs to pass parameters in the URL. I've got to the point where www.example.com/stats/?stat-name works - it takes me to my stats-pages.php template, where I can process it.
What I need to do is to clean up the URL so that the ? is removed, and the URL becomes www.example.com/stats/stat-name instead of www.example.com/stats/stats/?stat-name
In my functions.php, I have:
function add_query_vars($aVars) {
$aVars[] = "stats";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
In my .htaccess, I have
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^stats/^([A-Za-z0-9]+)/$ /stats=$1 [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Can anyone help please? Thanks!
The solution was as follows:
Referencing https://codex.wordpress.org/Rewrite_API/add_rewrite_rule for the example.
In functions.php:
In stats-pages.php, you can then do:
I removed my RewriteRule from .htaccess, as it was no longer needed.
IMPORTANT: You MUST flush and regenerate the rewrite rules within Wordpress (Settings -> Permalinks -> Save Changes) in order for the new URLs to work.
An URL such as https://example.com/stats/btts/ will then work and pass the stats parameter to the page.