wc_fix_rewrite_rules

Various rewrite rule fixes.

Description

(array) wc_fix_rewrite_rules( (array) $rules ); 

Returns (array)

Parameters (1)

0. $rules (array)
The rules.

Usage

  1. if ( !function_exists( 'wc_fix_rewrite_rules' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // The rules. 
  5. $rules = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_fix_rewrite_rules($rules); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-core-functions.php  
  1. function wc_fix_rewrite_rules( $rules ) { 
  2. global $wp_rewrite; 
  3.  
  4. $permalinks = wc_get_permalink_structure(); 
  5.  
  6. // Fix the rewrite rules when the product permalink have %product_cat% flag. 
  7. if ( preg_match( '`/(.+)(/%product_cat%)`' , $permalinks['product_rewrite_slug'], $matches ) ) { 
  8. foreach ( $rules as $rule => $rewrite ) { 
  9. if ( preg_match( '`^' . preg_quote( $matches[1], '`' ) . '/\(`', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) { 
  10. unset( $rules[ $rule ] ); 
  11.  
  12. // If the shop page is used as the base, we need to handle shop page subpages to avoid 404s. 
  13. if ( $permalinks['use_verbose_page_rules'] && ( $shop_page_id = wc_get_page_id( 'shop' ) ) ) { 
  14. $page_rewrite_rules = array(); 
  15. $subpages = wc_get_page_children( $shop_page_id ); 
  16.  
  17. // Subpage rules 
  18. foreach ( $subpages as $subpage ) { 
  19. $uri = get_page_uri( $subpage ); 
  20. $page_rewrite_rules[ $uri . '/?$' ] = 'index.php?pagename=' . $uri; 
  21. $wp_generated_rewrite_rules = $wp_rewrite->generate_rewrite_rules( $uri, EP_PAGES, true, true, false, false ); 
  22. foreach ( $wp_generated_rewrite_rules as $key => $value ) { 
  23. $wp_generated_rewrite_rules[ $key ] = $value . '&pagename=' . $uri; 
  24. $page_rewrite_rules = array_merge( $page_rewrite_rules, $wp_generated_rewrite_rules ); 
  25.  
  26. // Merge with rules 
  27. $rules = array_merge( $page_rewrite_rules, $rules ); 
  28.  
  29. return $rules;