Add Rewrite Rule for custom page

function add_my_rule() { global $wp; $wp->add_query_var(‘args’); $wp->add_query_var(‘arg_username’); add_rewrite_rule(‘writer/([0-9]+)/([^/]*)/page/([0-9]+)’,’index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]&paged=$matches[3]’,’top’); add_rewrite_rule(‘writer/([0-9]+)/([^/]*)’,’index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]’,’top’); /*global $wp_rewrite; $wp_rewrite->flush_rules();*/ } add_action(‘init’, ‘add_my_rule’); This should do the trick. One rewriterule for writer/user_id/username (username isn’t used in the rewriterule but is necessarily to make it work. The second rewriterule is the same except that it adds pagination. EDIT: added arg_username in code above.

How to prevent the default home rewrite to a static page

The redirect is thanks to redirect_canonical() – we can simply swoop in with a filter and disable it for the front page: function wpse_184163_disable_canonical_front_page( $redirect ) { if ( is_page() && $front_page = get_option( ‘page_on_front’ ) ) { if ( is_page( $front_page ) ) $redirect = false; } return $redirect; } add_filter( ‘redirect_canonical’, ‘wpse_184163_disable_canonical_front_page’ ); … Read more

Rewrite Rules for Multiple (more than 2) Taxonomies

You could try something like this: function custom_rewrite( $wp_rewrite ) { $feed_rules = array( ‘product-category/(.+)/material/(.+)/color/(.+)’ => ‘index.php?product_cat=”. $wp_rewrite->preg_index(1).”&pa_material=”. $wp_rewrite->preg_index(2).”&pa_color=”. $wp_rewrite->preg_index(3) “product-category/(.+)/color/(.+)’ => ‘index.php?product_cat=”. $wp_rewrite->preg_index(1).”&pa_color=”. $wp_rewrite->preg_index(2) “product-category/(.+)/material/(.+)’ => ‘index.php?product_cat=”. $wp_rewrite->preg_index(1).”&pa_material=”. $wp_rewrite->preg_index(2) ); $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules; } // refresh/flush permalinks in the dashboard if this is changed in any way add_filter( “generate_rewrite_rules’, ‘custom_rewrite’ ); It … Read more

Update URL Snippet to Canonical Permalink URL

Why? I think the reason why a) domain.com/123 is not redirected back to b) domain.com/123/post-slug with the /%post_id%/%postname%/ permalinks settings, is that the matched rule is: [matched_rule] => ([0-9]+)(/[0-9]+)?/?$ You can get that information from the global $wp object. There are other useful informations in $wpavailable after the wp action: Example (post): domain.com/123/post-slug/ [query_vars] => … Read more

Preventing index.php?category_name=something from redirecting

This should work: add_action( ‘init’, ‘wpa12742_init’ ); function wpa12742_init(){ add_rewrite_rule( ‘category/(.+?)/(\d{4})/?$’, ‘index.php?category_name=$matches[1]&year=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘category/(.+?)/(\d{4})/page/(\d+)/?$’, ‘index.php?category_name=$matches[1]&year=$matches[2]&paged=$matches[3]’, ‘top’ ); } EDIT On second thought, that’s not enough, since you’ll get caught by redirect_canonical(). Add this too: add_filter( ‘term_link’, ‘wpa12743_term_link’, 10, 3 ); function wpa12743_term_link( $link, $term, $taxonomy ){ if(‘category’ != $taxonomy && !preg_match( ‘@^\d{4}$@’, get_query_var(‘year’) … Read more

URL rewrite based on a custom field value

here is an idea, first add lid to query_vars: add_filter(‘query_vars’, ‘lid_query_vars’); function lid_query_vars($vars) { // add lid to the valid list of variables $new_vars = array(‘lid’); $vars = $new_vars + $vars; return $vars; } then use parse_request hook to create your redirect add_action(‘parse_request’, ‘lid_parse_request’); function lid_parse_request($wp) { // only process requests with “lid” if (array_key_exists(‘lid’, … Read more

How to custom change author base without $this->front?

You’re close, but you don’t need the add_rewrite_rule call. add_action(‘init’, ‘wpse82004_init’); function wpse82004_init() { global $wp_rewrite; $wp_rewrite->author_base=”u”; $wp_rewrite->author_structure=”https://wordpress.stackexchange.com/” . $wp_rewrite->author_base . ‘/%author%’; } After that’s in place, just re-save permalinks. Here’s a plugin to put an option on your permalinks page to do this.

Is it possible to change the URL of custom post types to hide the post type slug?

You need to use option ‘rewrite’ in your custom post type registration. E.g. ‘rewrite’ => array(‘slug’ => ‘products’), From the codex When you namespace a URL and still want to use a “clean” URL structure, you need to add the “rewrite” element to the array. For example, assuming the “ACME Widgets” example from above: add_action( … Read more