Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter leonardteo

    (@leonardteo)

    Nothing special in the .htaccess. Here it is:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    What I found helped is when I did a print_r of all the rewrite rules in this function (function wp_insertMyRewriteRules($rules)).
    Insert
    echo "<pre>".print_r($rules, true)."</pre>";

    That will help you to debug all the rewrite rules that are being added.

    Also, the order of the rewrite rules being added is important.
    E.g.

    $newrules['sermons/scripture/([^/]+)/chapter/?([0-9]{1,})/page/?([0-9]{1,})/?$'] = 'index.php?pagename=sermons/scripture&book=$matches[1]&chapter=$matches[2]&paged=$matches[3]';
    $newrules['sermons/scripture/([^/]+)/chapter/?([0-9]{1,})/?$'] = 'index.php?pagename=sermons/scripture&book=$matches[1]&chapter=$matches[2]';
    $newrules['sermons/scripture/([^/]+)/?$'] = 'index.php?pagename=sermons/scripture&book=$matches[1]';

    It will try to match the first rule and if it doesn’t match, then continue.

    Those were the difficult gotchas…

    Good luck,

    Leonard

    Thread Starter leonardteo

    (@leonardteo)

    I solved it. Here’s the final solution.

    In functions.php

    add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
    add_filter('query_vars','wp_insertMyRewriteQueryVars');
    add_filter('init','flushRules');
    
    // Remember to flush_rules() when adding rules
    function flushRules(){
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    
    // Adding a new rule
    function wp_insertMyRewriteRules($rules)
    {
    	$newrules = array();
    	$newrules['sermons/scripture/(.+)'] = 'index.php?pagename=sermons/scripture&book=$matches[1]';
    	$finalrules = $newrules + $rules;
            return $finalrules;
    }
    
    // Adding the var so that WP recognizes it
    function wp_insertMyRewriteQueryVars($vars)
    {
        array_push($vars, 'book');
        return $vars;
    }
    
    //Stop wordpress from redirecting
    remove_filter('template_redirect', 'redirect_canonical');

    In your page theme:

    $book = urldecode($wp_query->query_vars['book']);
Viewing 2 replies - 1 through 2 (of 2 total)