I’m attempting to write a plugin for WordPress but I’m having some issues with regards to the wp_rewrite functionality.
I want to make one page appear as its many by passing variables via the URL (such as: www.mysite.com/WordPress?variable=helloall)
However I want to keep the permalink structure intact, so i want the URL to appear as such:
www.mysite.com/WordPress/helloall
I then want to be able to take the slug and use that the search my database. (like you would using $_GET if I was using the general method i mentioned first)
I have found a few tutorials online and as of yet able to get this working. I believe my problem is due to a lack of understand with HOW you write the rules correctly.
I have used this tutorial:
http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
and I have attempted to use the same code for the most part. I am able to set the rules, but they just dont seem to want to work for me
can anyone tell me the correct format to beable to do this?
Edit
this is my current function
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(.?.+?)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action(‘generate_rewrite_rules’, ‘add_rewrite_rules’);
Solution
I have figured it out, I was going to post this as an answer but it seems I am unable to answer my own questions at this moment in time, so instead I’m editing the original post:
First off, the code I post above is correct, however the reason it was not working is because I was not flushing the rules, I do this with the following code:
function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'init', 'flush_rewrite_rules');
My new problem, was that my code worked a little to well, redirecting ALL pages instead of just the one I wanted, this meant that no child pages would display which is a bit of an issue, I have however solved the problem with one small edit:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(testpage)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
So my final code regarding the wp_rewrite functionality is as follows:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(testpage)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}
function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');
I hope this saves someone else some time in the future.
I figured out how to get it working on e a specific page name, this is for anyone who is having issues in the future: