Rewrite API issue – appears to have no affect on URL
-
I’ve created a custom, dynamic page template using ‘custom-page.php’ called ‘zip-page.php’ which takes a user entered zip code as input.
The user enters a zip code into an input field on another page and clicks [GO], which sends them to the ‘your_zip’ page via GET. The ‘your_zip’ page gets the city, state and county from SQL using the zip code and modifies the page content accordingly.
The resulting URL: (for zip code 20500)
https://www.example.com/your_zip/?zipCode=20500
The desired URL:
https://www.example.com/your_zip/20500
—
Here is the php from my functions.php:
—// URL rewrite rule for zip code pages add_filter( 'rewrite_rules_array','my_zip_page_rewrite_rules' ); add_filter( 'query_vars','my_zip_page_query_vars' ); //perform flush rules one time for site each time rules change add_action( 'wp_loaded','my_zip_page_flush_rules' ); // flush_rules() if our rules are not yet included function my_zip_page_flush_rules() { $rules = get_option( 'rewrite_rules' ); if ( ! isset( $rules['your_zip/([^/]+)/?$'] ) ) { global $wp_rewrite; $wp_rewrite->flush_rules(); } } // Adding new zip_page rule function my_zip_page_rewrite_rules( $rules ) { $newrules = array(); $newrules['your_zip/([^/]+)/?$'] = 'your_zip/?zipCode=$matches[1]'; return $newrules + $rules; } // Adding the zipCode var so that WP recognizes it function my_zip_page_query_vars( $vars ) { array_push($vars, 'zipCode'); return $vars; }
—
I’ve tried several iterations of different rewrite rules with no success.
(`// // DRAFT:RewriteRule ^your_zip/([0-9]+) your_zip/?zipCode=$1 [L]
// // DRAFT:RewriteRule ^your_zip/(\d+)$ your_zip/?zipCode=$1 [L]
// // DRAFT:RewriteRule ^your_zip/?zipCode=(\d+)$ your_zip/$1 [L]
// // DRAFT:RewriteRule ^your_zip/(\d{3,5})$ your_zip/?zipCode=$1 [L]
// // DRAFT:RewriteRule ^your_zip/?zipCode=(\d{3,5})$ your_zip/$1 [L]
`)
—
- The topic ‘Rewrite API issue – appears to have no affect on URL’ is closed to new replies.