flush_rules(); only works when there is an echo in function
-
hey guys,
I got a very strange problem. I want to add some custom rewrite-rules to my theme in order to get custom pretty permalinks. I must pass variables in the URL so I can adjust the content according to what the user selects in a submenu. In order to do this I use pretty much the standard procedure as described in the Codex.
It actually works, BUT ONLY if I add a php echo to the function my_flush_rules where I call $wp_rewrite->flush_rules().
If I leave out the echo or just echo an empty string it stops working. In this case no more variables are passed on and the urls aren’t rewritten anymore, so I suppose the new URL-rewrite rules simply aren’t applied.As you may have guessed I do not want to echo some strange text just so url-rewriting works properly. What did I do wrong?
I’m totally stuck here, can anyone help?
Here’s my code from the functions.php:
add_filter('rewrite_rules_array', 'my_rewrite_rules'); add_filter('query_vars','my_rewrite_query_vars'); add_filter('init','my_flush_rules'); function my_flush_rules(){ echo "whatever"; global $wp_rewrite; $wp_rewrite->flush_rules(); } function my_rewrite_rules( $rewrite_rules ) { $new_rules = array( '(test)/([a-z]+)$' => 'index.php?pagename=$matches[1]&mycategoryvar=$matches[2]', '(test)/([0-9]+)$' => 'index.php?pagename=$matches[1]&mypagevar=$matches[2]', '(test)/([a-z]+)/([0-9]+)$' => 'index.php?pagename=$matches[1]&mycategoryvar=$matches[2]&mypagevar=$matches[3]' ); $rewrite_rules = $new_rules + $rewrite_rules; return $rewrite_rules; } function my_rewrite_query_vars($qvars) { array_push($qvars, 'mypagevar', 'mycategoryvar'); return $qvars; }
And here’s my code from the .htaccess (I currently develop the page on my local XAMPP installation – hence the url):
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /wordpress/ RewriteCond %{REQUEST_URI} !\..+$ RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) https://localhost/wordpress/$1/ [R=301,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wordpress/index.php [L] </IfModule>
- The topic ‘flush_rules(); only works when there is an echo in function’ is closed to new replies.