• 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>
Viewing 1 replies (of 1 total)
  • Thread Starter b-lame

    (@b-lame)

    getting there, but still need help:
    According to this thread https://www.ads-software.com/support/topic/376238/page/2?replies=40 some people are having the same problem, but there doesn’t seem to be a solution yet.

    The problem: WP seem to omit my custom query_vars when redirecting. When I navigate to i.e. localhost/wordpress/test/2/ the redirection SHOULD convert that to localhost/wordpress/test/?mypagevar=2 internally.
    Instead it takes me straight to localhost/wordpress/test/ and omits the variables. The code DOES work if I enter the url localhost/wordpress/test/?mypagevar=1 directly.

    I do resave permalinks on admin panel everytime and i tried printing the rewrite rules array and the query vars array to see if my custom stuff is there.
    But: As soon as I print out one of these arrays OR simply put a php echo in one of the filter-functions everything works just fine. If I omit the echo or the print_r it doesn’t work anymore. I don’t get this AT ALL. Any suggestions?

Viewing 1 replies (of 1 total)
  • The topic ‘flush_rules(); only works when there is an echo in function’ is closed to new replies.