First, create a single states
page.
Add a query var to hold the state value, wpd_state
:
function wpd_query_vars( $qvars ) {
$qvars[] = 'wpd_state';
return $qvars;
}
add_filter( 'query_vars', 'wpd_query_vars' , 10, 1 );
Add a rewrite rule to capture the state from the URL, set the wpd_state
query var, and load the page states
:
function wpd_rewrite_rule() {
add_rewrite_rule(
'states/([^/]+)/?$',
'index.php?pagename=states&wpd_state=$matches[1]',
'top'
);
}
add_filter( 'init', 'wpd_rewrite_rule' );
You can then use get_query_var( 'wpd_state' )
to fetch the requested state, which you can use to pass to your enqueued javascript via wp_localize_script
.
Related Posts:
- How to create a front end user profile with a friendly permalink
- Masking wp-content/themes/name/images to just images directory using htaccess
- Using custom/dynamic “slug” for a page
- How to make pages slug have priority over any other taxonomies like custom-post, post or category
- Rewrite URL – how to do a SEO-friendly Unicode custom URL?
- Clash of the rewrites
- Query Vars for the Homepage?
- Disable wordpress pagination URL rewrite for specific page
- How to use Post Custom Metadata in Post Titles and Post Permalinks
- Using WordPress with Apache behind an nginx reverse proxy
- How do i change the search permanent links
- add_rewrite_rule() not playing nice with child pages
- rewrite_rule() not preserving the query string
- Appending numbers to url do not break the link
- WordPress Network on IIS7/SQL Server – Rewrite Issues
- Custom comments
- How to Change Author Posts URL
- Change Attachment Post URLs to File URLs
- WordPress 404 unless there is a space in url
- Change permalink for default archive – yearly
- Migrating a page to be an article
- How to get “extended” path info from URL in a plugin
- Custom URL routing based on cookie value
- Rewriting rules: difference between ‘init’, ‘rewrite_rules_array’, ‘generate_rewrite_rules’?
- How to perform a query at the URL?
- url rewriting for most recent post
- Is there a way to create alias without using htacess, permalinks or delving into code?
- Remove “/page/1” from the URL
- hard flush_rewrite_rules() not regenerating .htaccess
- Remove Query String from URL on Redirect – Redirection Plugin
- Nginx rewrite rules
- Cleanup URL for a custom page in wordpress
- Rewrite Rules Are Redirecting and Not Passing VARs
- WordPress rewrite rules don’t need ^?
- add_rewrite_rule fro html to another url not working
- Adding rewrite rules directly to .htaccess file
- How do I redirect to a non-www version and make it the default URL?
- WordPress redirects non-existing url to existing ones – how to disable
- How to rewrite url wordpress?
- WordPress Redirecting Non Category Pages /page/nnn to their Canonical URLs
- How to create a profile page for specfic / custom user role members?
- Can I change default registration link (without htaccess)?
- A clean custom rewrite rule for weekly archives
- I want to add a new tag to WordPress URL permastruct
- How to remove trailing slash from root WordPress folder?
- Rewriting the URL of a custom post to a certain page
- rewrite wordpress page url for seo friendly query strings
- Persist url parameter passed by a referrer website? [duplicate]
- ReWrite rule for files of a previously standalone blog moved to network
- Fixing custom rewrite rule
- htaccess rewrite ignored
- Best way to trigger rewrite rules
- Trouble with question mark in rewrite rule
- Adding a query string to only one page url
- Url rewrite with htaccess
- force www rewrite if wordpress put in a folder
- Current post ID – relative url
- How to use wp rewrite to hide form action url?
- WordPress Rewrite rule is not working
- change the url in wordpress
- Redirect URL to an existing page with query string or #! added on
- Add extra optional text to permalink e.g. “-with-*”
- change url for portfolio image
- remove archive from url :: marketpress
- add_rewrite_rule with optional parameters
- How to change url of specific posts with name of a root page
- Why wordpress rewrite my custom URL?
- Rewrite rule regex help required
- Redirect empty search to another page
- Rewrite dynamic URLs as pretty URLs in WordPress functions.php (add_rewrite_rule) – what’s wrong with my code?
- WordPress URL redirect and replace ? question mark
- Change author base and slug in author link
- Rewrite rule not working, suspect redirect string
- WordPress index in subdirectory, blogposts in root directory
- Fresh install redirects to www, breaking page loads
- Rewrite Url using .htaccess or hook
- url restructure or rewrite having $_GET variables
- How to use a RewriteRule to change endpoint url
- Custom taxonomy Rewrite Rule
- taxonomy term in URL slug won’t forward to the correct term for custom post like it does for default categories/normal posts
- Error 404 wordpress redirecting URL
- I want to add one extra field to my URLs, I’m using Rewrite functions but it doesn’t work
- .htaccess RewriteRule to include post type and taxonomy
- What’s the best approach to do this?
- Font Page overrides rewrites
- WordPress Rewrite: Ignore Specific Pattern
- rewrite rule for home/index page
- WPNetwork with custom URL Rewrite rules
- Stop wordpress to redirecting home page if no page found
- Additional .htaccess rules based on wp page
- URL Rewriting in WordPress
- Rewrite URL variable to custom path
- Own code on index.php wordpress theme file, help with the rewrite rules
- URL rewrites af
- How to change search url produced by ‘s GET method?
- Hide / rewrite download link
- Lost WordPress website access after changing URL [closed]
- How to make custom WordPress page deliver search results
- URI rewriting: handling one page
- Problems Implementing Non-WordPress Rewrite Rules