Advertisement
helgatheviki

Brent Shepherd's Rewrite Rule generator

Mar 18th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. /**
  2.  * Generates all the rewrite rules for a given post type.
  3.  *
  4.  * The rewrite rules allow a post type to be filtered by all possible combinations & permutations
  5.  * of taxonomies that apply to the specified post type and additional query_vars specified with
  6.  * the $query_vars parameter.
  7.  *
  8.  * Must be called from a function hooked to the 'generate_rewrite_rules' action so that the global
  9.  * $wp_rewrite->preg_index function returns the correct value.
  10.  *
  11.  * @param string|object $post_type The post type for which you wish to create the rewrite rules
  12.  * @param array $query_vars optional Non-taxonomy query vars you wish to create rewrite rules for. Rules will be created to capture any single string for the query_var, that is, a rule of the form '/query_var/(.+)/'
  13.  *
  14.  * @author Brent Shepherd <me@brentshepherd.com>
  15.  * @since 1.0
  16.  */
  17. function eg_generate_rewrite_rules( $post_type, $query_vars = array() ) {
  18.     global $wp_rewrite;
  19.  
  20.     if( ! is_object( $post_type ) )
  21.         $post_type = get_post_type_object( $post_type );
  22.  
  23.     $new_rewrite_rules = array();
  24.  
  25.     $taxonomies = get_object_taxonomies( $post_type->name, 'objects' );  
  26.  
  27.     // Add taxonomy filters to the query vars array
  28.     foreach( $taxonomies as $taxonomy )
  29.         $query_vars[] = $taxonomy->query_var;
  30.  
  31.     // Loop over all the possible combinations of the query vars
  32.     for( $i = 1; $i <= count( $query_vars );  $i++ ) {
  33.  
  34.         $new_rewrite_rule =  $post_type->rewrite['slug'] . '/';
  35.         $new_query_string = 'index.php?post_type=' . $post_type->name;
  36.  
  37.         // Prepend the rewrites & queries
  38.         for( $n = 1; $n <= $i; $n++ ) {
  39.             $new_rewrite_rule .= '(' . implode( '|', $query_vars ) . ')/(.+?)/';
  40.             $new_query_string .= '&' . $wp_rewrite->preg_index( $n * 2 - 1 ) . '=' . $wp_rewrite->preg_index( $n * 2 );
  41.         }
  42.  
  43.         // Allow paging of filtered post type - WordPress expects 'page' in the URL but uses 'paged' in the query string so paging doesn't fit into our regex
  44.         $new_paged_rewrite_rule = $new_rewrite_rule . 'page/([0-9]{1,})/';
  45.         $new_paged_query_string = $new_query_string . '&paged=' . $wp_rewrite->preg_index( $i * 2 + 1 );
  46.  
  47.         // Make the trailing backslash optional
  48.         $new_paged_rewrite_rule = $new_paged_rewrite_rule . '?$';
  49.         $new_rewrite_rule = $new_rewrite_rule . '?$';
  50.  
  51.         // Add the new rewrites
  52.         $new_rewrite_rules = array( $new_paged_rewrite_rule => $new_paged_query_string,
  53.                                     $new_rewrite_rule       => $new_query_string )
  54.                              + $new_rewrite_rules;
  55.     }
  56.  
  57.     return $new_rewrite_rules;
  58. }
  59.  
  60.  
  61.  
  62. // add the rules
  63. function mv_add_rewrite_rule($wp_rewrite){
  64.     $new_rules = eg_generate_rewrite_rules('product');
  65.     $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  66. }
  67. add_action('generate_rewrite_rules','mv_add_rewrite_rule',99);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement