Created
March 7, 2013 07:05
-
-
Save MaraScott/5106105 to your computer and use it in GitHub Desktop.
Name : add_rewrite_rules() - Language : PHP - type : function - Platform : wordpress - tag : createRewriteRules, add_rewrite_tag, rewrite_rules, generate_rewrite_rules, query_vars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // see : http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html | |
| /***************** REWRITE RULES *****************/ | |
| /*** create Generic rewrite rules ***/ | |
| function createRewriteRules() { | |
| global $wp_rewrite; | |
| // add rewrite tokens | |
| $keytag = '%tag%'; | |
| $wp_rewrite->add_rewrite_tag($keytag, '(.+?)', 'tag='); | |
| $keywords_structure = $wp_rewrite->root . "tag/$keytag/"; | |
| $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure); | |
| $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules; | |
| return $wp_rewrite->rules; | |
| } | |
| add_action('generate_rewrite_rules', 'createRewriteRules'); | |
| /*** create custom rewrite rules ***/ | |
| // add new query var | |
| function query_vars($public_query_vars) { | |
| $public_query_vars[] = "ypsearch"; | |
| $public_query_vars[] = "yppage"; | |
| /* Note: you do not want to add a variable multiple times. As in | |
| the example above, multiple rules can use the same variables | |
| */ | |
| return $public_query_vars; | |
| } | |
| add_filter('query_vars', 'query_vars'); | |
| // create new rewrite rule | |
| function add_rewrite_rules( $wp_rewrite ) | |
| { | |
| // $template_page_name = getTemplatePageName(); // This is my function you can ignore it | |
| $template_page_name = 'brand'; | |
| $new_rules = array( | |
| 'brand/(.+)/(.+?)/?$' => 'index.php?s='.$wp_rewrite->preg_index(1).'&newquery='.$wp_rewrite->preg_index(2) | |
| ); | |
| // Always add your rules to the top, to make sure your rules have priority | |
| $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; | |
| } | |
| add_action('generate_rewrite_rules', 'add_rewrite_rules'); | |
| /*** check rewriterules ***/ | |
| // you can use this plugin http://wordpress.org/extend/plugins/rewrite-rules-inspector/ | |
| // OR this function | |
| function getRewriteRules() { | |
| global $wp_rewrite; // Global WP_Rewrite class object | |
| return $wp_rewrite->rewrite_rules(); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment