Making extra parameters optional

Two points: Your rule isn’t particularly specific. For numeric matches you should be specific about it and specify a) digits and b) how many digits. Year would be ([0-9]{4}), month/day would be ([0-9]{1,2}). You can’t do it with one rule. Add three separate rules instead. add_rewrite_rule( ‘whats-on/([0-9]{4})/?$’, ‘index.php?page_id=71&event_year=$matches[1]’,’top’); add_rewrite_rule( ‘whats-on/([0-9]{4})/([0-9]{1,2})/?$’, ‘index.php?page_id=71&event_year=$matches[1]&event_month=$matches[2]’,’top’); add_rewrite_rule( ‘whats-on/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$’, ‘index.php?page_id=71&event_year=$matches[1]&event_month=$matches[2]&event_day=$matches[3]’,’top’);

Clean URLs for custom $_GET variables

You should look at “Rewrite Endpoints”, which are much simpler to work with than custom rewrites. And they fit your use case perfectly: http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint You might add your “show” endpoint like so: function wpsx_37961_add_endpoints() { // Add the “show” rewrite endpoint to all URLs add_rewrite_endpoint(‘show’, EP_ALL); } add_action(‘init’,’wpsx_37961_add_endpoints’); Then you can check for its value … Read more

Cyrillic characters in rewrite rules cause 404 Not Found errors

I ended up following @Kolya Korobochkin advice and added uppercase and lowercase versions of the rewrite rules that include escaped octets. $regular_page_uri = get_page_uri( $page->ID ); $uppercase_page_uri = preg_replace_callback( ‘/%[0-9a-zA-Z]{2}/’, create_function( ‘$x’, ‘return strtoupper( $x[0] );’ ), $regular_page_uri ); The Percent Encode Capital Letter plugin uses a similar approach to convert the octets in every … Read more

Case-insensitive add_rewrite_rules in WordPress functions

As the answer below mentions, it is not possible to pass a flag to add_rewrite_rule(); however, it is possible to use an inline modifier. In your example, you would do this: $aNewRules = array(‘(?i)my-books/?$’ => ‘index.php?pagename=my-books’); (note the (?i) in the regular expression). The advantage of this approach is that your rewrite rules are much … Read more

WordPress Custom URL Rewrites

I do something similar with custom records called shops and their specific ID number. In my case I want http://example.com/shops/UNIDrecordthatsreallylong to load with the variable shop_unid being the UNID record. Here’s my working function: //Shop page rewrites function shop_rewrite() { add_rewrite_rule(‘^shops/([^/]*)/?’,’index.php?pagename=shops&shop_unid=$matches[1]’,’top’); add_rewrite_tag(‘%shop_unid%’,'([^&]+)’); } add_action(‘init’, ‘shop_rewrite’); Note that I used pagename instead of the post number. … Read more

Flush_rewrite_rules not working when settings updated

Does this work for you? Seems to work when I try it. function qd_settings_api_init() { add_settings_section( ‘qd_theme_setting_section’, ‘Theme Reading Settings’, ‘qd_theme_setting_section_callback_function’, ‘reading’ ); add_settings_field( ‘qd_news_page’, ‘News Page’, ‘qd_news_page_dropdown_cbf’, ‘reading’, ‘qd_theme_setting_section’ ); if (delete_transient(‘qd_flush_rules’)) flush_rewrite_rules(); // * Added register_setting( ‘reading’, ‘qd_news_page’, ‘qd_sanitize’ ); // * Changed } /* Added this function. */ function qd_sanitize($input) { set_transient(‘qd_flush_rules’); … Read more