Skip to main content

Well once again I had to do something in wordpress that I wasn’t sure off. I bit of googling and reading a few blogs I got the solutions.
At first you would think that it isn’t possible buuut it is. WordPress is just a little stingy of unknown variables being passed without it knowing.. anyway… here is how it is done. first the code:

function add_query_vars($aVars) {
    $aVars[] = "sid";    // represents the name of the product category as shown in the URL
    return $aVars;
}

add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($aRules) {
    $aNewRules = array('societies/([^/]+)/?$' => 'index.php?pagename=societies&sid=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

Explained:

function add_query_vars($aVars) {
    $aVars[] = "myvar";    // represents the name of the variable as shown in the URL
    return $aVars;
}

add_filter('query_vars', 'add_query_vars');

The function part tells wordpress that you are going to pass a variable through to it via the url. “myvar” will be the name of you variable so basicly you url will be : “http://www.wordpresssite.com?page_id=3&myvar=myvalue”. add_cation just initializes the function for wordpress to see it.

function add_rewrite_rules($aRules) {
    $aNewRules = array('mypage/([^/]+)/?$' => 'index.php?pagename=mypage&myvar=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

This function basicly tells wordpress the following:
1. It will check for the page name. So you have to specify what page it will be on. So if you install your plugin on the page called mypage. the permalink will be http://www.wordpressite.com/mypage/
2. It tells wordpress that everything after that pagename will be the VALUE of the variable NAME set in the previous function.

TAKE NOTE:
You must go to settings->permalinks and save changes before changes will be applied.

To Implement this you dump that code into your function.php (if you are going to use this in a template) or you plugin file (if you will use it in your plugin). Then in your code you can reference a link called http://www.wordpresssite.com/mypage/myvarValue/.

If you set the “add_query_vars” function to myvar your query string would normally look like:
http://www.wordpressite.com/?page_id=3&myvar=myvalue.

But now it will look like:
http://www.wordpresssite.com/mypage/myvalue.

I hope this is explained properly. If you have any questions. Please ask.

Update:

The filter “rewrite_rules_array” is to rewrite the vars to look better. To be able to see all the rules currently set add the following function to you file:

function getRewriteRules() {
    global $wp_rewrite; // Global WP_Rewrite class object
    return $wp_rewrite->rewrite_rules();
}

then you can just add:

 print_r(getRewriterules()) 

anywhere to display the rules.

 

Janes Oosthuizen

Author Janes Oosthuizen

Programmer and Tech Junky from Cape Town, South-Africa. I have been programming for more than 15 years in various languages including ( CSS, HTML, javascript, PHP, MySQL, Wordpress and many other ).

More posts by Janes Oosthuizen

Join the discussion 23 Comments

Leave a Reply