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?
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');
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.
Rewrite rules are how WordPress creates clean / pretty URIs from URL query parameters. When your new page or blog post automatically gets a human-friendly URL, this is provided by a rewrite rule, which itself is using WordPress's Rewrite API.
There are really two sets of rewrite rules. One contained in . htaccess which is written by save_mod_rewrite_rules() and used by the HTTP server. There is another set maintained and used by the PHP server itself.
To flush WordPress rewrite rules or permalinks (usually needs to be done manually for new custom post types) from the admin Dashboard: Step 1: In the main menu find "Settings > Permalinks". Step 2: Scroll down if needed and click "Save Changes". Step 3: Rewrite rules and permalinks are flushed.
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:
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With