wc_fix_rewrite_rules( array $rules )
Various rewrite rule fixes.
Description Description
Parameters Parameters
- $rules
-
(Required) Rules.
Return Return
(array)
Source Source
File: includes/wc-core-functions.php
function wc_fix_rewrite_rules( $rules ) { global $wp_rewrite; $permalinks = wc_get_permalink_structure(); // Fix the rewrite rules when the product permalink have %product_cat% flag. if ( preg_match( '`/(.+)(/%product_cat%)`', $permalinks['product_rewrite_slug'], $matches ) ) { foreach ( $rules as $rule => $rewrite ) { if ( preg_match( '`^' . preg_quote( $matches[1], '`' ) . '/\(`', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) { unset( $rules[ $rule ] ); } } } // If the shop page is used as the base, we need to handle shop page subpages to avoid 404s. if ( ! $permalinks['use_verbose_page_rules'] ) { return $rules; } $shop_page_id = wc_get_page_id( 'shop' ); if ( $shop_page_id ) { $page_rewrite_rules = array(); $subpages = wc_get_page_children( $shop_page_id ); // Subpage rules. foreach ( $subpages as $subpage ) { $uri = get_page_uri( $subpage ); $page_rewrite_rules[ $uri . '/?$' ] = 'index.php?pagename=' . $uri; $wp_generated_rewrite_rules = $wp_rewrite->generate_rewrite_rules( $uri, EP_PAGES, true, true, false, false ); foreach ( $wp_generated_rewrite_rules as $key => $value ) { $wp_generated_rewrite_rules[ $key ] = $value . '&pagename=' . $uri; } $page_rewrite_rules = array_merge( $page_rewrite_rules, $wp_generated_rewrite_rules ); } // Merge with rules. $rules = array_merge( $page_rewrite_rules, $rules ); } return $rules; }
Changelog Changelog
Version | Description |
---|---|
2.2 | Introduced. |