• Resolved leonardteo

    (@leonardteo)


    Hi guys,

    I’m trying to pass a variable through the permalink URL structure, and do a custom query once the variable is passed.

    Basically, I have this:
    http://peoplesmontreal/sermons/scripture/{book}

    /sermons/scripture will direct to a page (already set up)
    {book} is the variable I’m trying to pass, which could be any of the books of the Bible.

    I’m having a heck of a time trying to get wordpress’ rewrite to recognize the new rule and read the variable.

    I’ve followed this tutorial (http://codex.wordpress.org/Function_Reference/WP_Rewrite) pretty much to the letter and getting a “Not found” on the page. I feel like I’m stabbing in the dark here…

    Here’s what I have 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=scripture&book=genesis';
    
    	$finalrules = $newrules + $rules;
    
            echo "Rewrite Rules: <pre>".print_r($finalrules, true)."</pre>";
    
            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');
Viewing 5 replies - 1 through 5 (of 5 total)
  • 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']);

    hi leonardteo, can you show if the htaccess has some special intructions or something else? i tested with the example and nothing, the wp clean the attribute when i go with this with a value inside it
    Thanks for your help

    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

    I am trying to pass numeroues variables to the rewrite method. I have two pages or more I would like to do it. I got one working but don’t know how to do two. I think your post to your solution up there might work but am confused `function add_query_vars($aVars) {
    $aVars[] = “aerial_cat”;
    $aVars[] = “flight_cat”; // represents the name of the product category as shown in the URL
    return $aVars;
    }

    function add_rewrite_rules($aRules) {
    $aNewRules = array(‘aerial-photography/([^/]+)/?$’ => ‘index.php?pagename=aerial-photography&aerial_cat=$matches[1]’);
    $aNewRules = array(‘flight-school/([^/]+)/?$’ => ‘index.php?pagename=flight-school&flight_cat=$matches[1]’);

    $aRules = $aNewRules + $aRules;
    return $aRules;
    echo “Rewrite Rules: <pre>”.print_r($aRules, true).”</pre>”;
    }
    add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);
    `
    tell me what you think please it would be a nice tool when thinking of solutions

    Ok I figured it out. I didn’t understand how rewrite worked. For people who need help doing more then one page it is simple. Just add another rule.

    $bNewRules = array('flight-school/([^/]+)/?$' => 'index.php?pagename=flight-school&flight_cat=$matches[1]');
        $aRules = $aNewRules + $bNewRules + $aRules;
        return $aRules;
    }

    I used

    function get_rewrite_urls(){
       global $wp_rewrite;  
    
            return $wp_rewrite->wp_rewrite_rules(); /* Returns an array */
        }  
    
       print_r(get_rewrite_urls());

    and I put it on the page that I was trying to see if the rewrite was working on. If you did it properly you should see your rewrite variable in code. Mine was in the front. Be sure to add the flush or else you won’t see it there and will spend numerous times refreshing the page wondering why it isn’t showing up on the print_r.

    ‘function add_query_vars($aVars) {
    $aVars[] = “aerial_cat”; // represents the name of the product category as shown in the URL
    $aVars[] = “flight_cat”;
    return $aVars;
    }’

    be sure to add the new variable to your add_query_vars. I hope this helps more people on this topic!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Passing variables through permalink url’ is closed to new replies.