Custom URL rewrites work, but break my permalinks

Well I solved it – in a rather unsatisfactory way – by explicitly re-defining the default posts rule in my own code. i.e. $rewrite_keywords_structure = $wp_rewrite->root.”/%year%/%monthnum%/%postname%/”; ..etc.. I don’t really understand why this is necessary, but there you go.

Optimal code for two add_rewrite_rule’s

This is the working code: add_action(‘init’, ‘rewrite_init’); // Rewrite add_action(‘query_vars’, ‘rewrite_query_vars’); add_filter(‘template_include’, ‘rewrite_template_include’); function rewrite_init(){ add_rewrite_rule(‘([^/]+)/([0-9]+)/?$’, ‘index.php?catname=$matches[1]&currentpage=$matches[2]’, ‘top’); add_rewrite_rule(‘cam/([^/]+)/?$’, ‘index.php?name=cam&perf=$matches[1]’, ‘top’); } function rewrite_query_vars($query_vars){ $query_vars[] = ‘currentpage’; $query_vars[] = ‘catname’; $query_vars[] = ‘perf’; return $query_vars; } function rewrite_template_include($template){ if(get_query_var(‘currentpage’) || get_query_var(‘catname’)){ $template = locate_template(array(‘category.php’)); } elseif(get_query_var(‘perf’)){ $template = locate_template(array(‘single.php’)); } return $template; }

Simple page URL rewrite with add_rewrite_rule()

The second argument of add_rewrite_rule is not a path, it the query string you want WordPress executes. This should work for example.com/sub-folder/thepage/ (change page_id with ID of your page): add_rewrite_rule(‘thepage/’, ‘index.php?page_id=12’); or with page slug: add_rewrite_rule(‘thepage/’, ‘index.php?pagename=thepage’); As you have WordPress in a subdirectory and you want to rip out the directoy name, you have … Read more

Does it still make sense using json endpoint ep_mask now that there’s the new rest api? [closed]

I think you should stop using rewrite endpoints to handle JSON responses. Instead you can and you should use the REST API. So, instead of this: function makeplugins_add_json_endpoint() { add_rewrite_endpoint( ‘json’, EP_PERMALINK | EP_PAGES ); } add_action( ‘init’, ‘makeplugins_add_json_endpoint’ ); And then handle the JSON response at your own, you can and you should do … Read more

Rewrite rule : custom post type with 2 numeric variiables

add_rewrite_rule() cannot automatically create that permalink structure for you. You should use wp_link_pages_link filter. E.g: add_filter(‘wp_link_pages_link’, function($link, $i) { global $post, $photo_id; if ($photo_id && ‘gallery’ === $post->post_type) $link = $link . “https://wordpress.stackexchange.com/” . intval($photo_id) . “https://wordpress.stackexchange.com/”; return $link; }, PHP_INT_MAX, 2); Then, you must use add_rewrite_tag() to make WordPress aware of photo query string: … Read more

Optional all capture groups in rewrite rule

Ok after 4 hours I managed to make it work function pl_add_rewrite_rule() { // this works with pagination too add_rewrite_rule(‘^slike(\/([a-z]+))?(\/([a-z]+))?(\/page\/([0-9]+))?$’,’index.php?page_id=45893&carstvo=$matches[2]&kategorija=$matches[4]&paged=$matches[6]’,’top’); add_rewrite_tag(‘%carstvo%’,'([^/]*)’); add_rewrite_tag(‘%kategorija%’,'([^/]*)’); } add_action(‘init’, ‘pl_add_rewrite_rule’, 10, 0);

Add Rewrite Endpoint to CPT Archive

You’re getting the 404 most likely because your custom taxonomy is not assigned to the endpoint mask (EP_PAGES) used with your endpoint. Because add_rewrite_endpoint() does work with custom post types and taxonomies, but you need to assign the endpoint mask to the post type or taxonomy during registration, via the ep_mask key in the rewrite … Read more

Do I need to flush rewrite rules when creating new user if using custom author rewrite rules?

Yes, you should flush rewrite rules in this case, because you add rules for every author based on his nicename. To be more precise, you should flush them after any author changes his nicename also. And you do this using author_rewrite_rules hook, which is fired in wp_rewrite_rules() only when there are no rules in database. … Read more