I am trying to write a plugin for WordPress, but I have some problems regarding wp_rewrite functions.
I want one page to be displayed as there are many, passing variables through a URL (for example: www.mysite.com/WordPress?variable=helloall)
However, I want to keep the constant link structure intact, so I want the URL to display as such:
www.mysite.com/WordPress/helloall
Then I want you to be able to use slug and use this search in my database. (for example, you would use $ _GET if I would use the first method that I mentioned)
I found several manuals on the Internet and am still able to get this job. I believe that my problem is due to a lack of understanding of HOW you write the rules correctly.
I used this tutorial:
http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
and I tried to use the same code for the most part. I can set the rules, but they just don't want to work for me.
can someone tell me the correct format 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) );
add_action ('generate_rewrite_rules', 'add_rewrite_rules');
Decision
I figured this out, I'm going to post this as an answer, but it seems like I can't answer my own questions at this point in time, so instead I edit the original post:
Firstly, the code that I publish above is correct, however, the reason why it did not work is that I did not flush 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 bit fine, redirecting ALL pages instead of what I wanted, this meant that no child pages would be displayed, which is the problem, but I 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) );
So, my final code regarding the wp_rewrite function 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 in the future.