I’ve been reading through this question and answer with great interest as I’m trying to achieve something very similar, but I’m failing and was wondering if someone could help me out.
I am also using a custom plug which is querying a custom table in order to populate a page. The page is a normal WP page with a template attached that calls a function to my plug-in file to get data.
If I type this in the URL I get my page loading properly:
www.mywebsite.com/?pagename=products/food-and-catering-workwear&prodid=232&pname=Trilby
Hat
I would like to type this (or use it in a link) and get the same result:
mywebsite.com/products/food-and-catering-workwear/232/Trilby
Hat/
(BTW, I also was thinking the reverse about rewrite_rules at first; that it would actually ‘change’ my URL to the more ‘friendly’ version once in place 🙂
I have added this line to the init function in my plug-in file (one thing is that I’m using this same init function to enqueue some js — I was assuming it would bo okay to just add this line to the same function):
add_rewrite_rule('([^/]+)/([^/]+)/([^/]+)/?','index.php?pagename=$matches[1]&prodid=$matches[2]&pname=$matches[3]','top' );
Note: in my page hierarchy there will always be a parent page and a child page in the slug; chances are the parent will always be the same page, but the child will definitely vary. But I’d like to keep the rule flexible to catch all possibilities. The main trigger for my plug-in will be these two variables in the query string.
I’ve also added prodid and pname to $query_vars:
add_filter( 'query_vars', 'prod_query_vars' );
function prod_query_vars( $query_vars )
{
$query_vars[] = 'prodid';
$query_vars[] = 'pname';
return $query_vars;
}
Does anyone see where I’m going wrong? One other thing. I’m not understanding the rule- flushing part. Since this is in a plug-in (not functions.php) when do I need to flush the rules? Each time I’ve updated the plugin file (trying out different code possibilities), I’ve first gone to the Permalinks page before trying to use my ‘friendly’ URL format. I noticed in one of the examples from the previous question: flush_rewrite_rules(false) was placed in the register_activation_hook() function, but my plug-in is already set up and running. I’m trying to alter it, so I’m thinking at this point I wouldn’t be using this (but maybe this would be a solution for future ‘installs’ of the plug-in)
I’m also not that versed in regular expressions, so I know it could be there where I’m going astray.
Sorry for the long-winded question, but I wanted to supply as much detail to my situation as possible.
Thanks in advance to anyone who can help.
In regards to when you need to be flushing the rewrite rules:
This should only happen once, make sure you are not calling
flush_rules()
on init, as this will really kill performance for high traffic sites.WordPress stores all of the rewrites in an array in the database, so all you need to do is hook in just before it updates the database. One way to do it is hook into “rewrite_rules_array” and add your rule:
Keep your ‘query_vars’ hook as it is. You will need to visit the Permalinks page to insert your rule into WordPress’ options – but then you should be set.
If it doesn’t work, you can hook into ‘parse_request’ to see which rewrite rule was chosen for the page you are viewing:
Also, your regex is a but loose for the structure you put above
mywebsite.com/products/food-and-catering-workwear/232/Trilby Hat/
, it would be more like:Is says “starts with ‘products’, ‘/’, then any word, ‘/’, then any numbers, ‘/’, then any word” (“$” means that the string has to now end)
I hope that helps!
Okay, after looking at this and looking at this, I realised that my problem was NOT with the rewrite rules. It was with how my plug was dealing with the query string!!! It was looking for $_GET[‘prodid’] to populate the page. I did not realise that by using the “query_vars” hook, I need to be using $prodid (as long as I declared it as global first).
Anyway, it’s sorted now and I thank Joe again for his help as well those involved in the first (linked) question, as that provided me with a lot of assistance too.