WordPress – can I get rewrite rules for a given path?

I am writing a plugin, which routes URLs to a custom format.

Quite simply put I want to get rewrite rules for a given path, can I do this?

Read More

so for example, I need a function as follows:

$path = 'team/john-smith';

$rules =  get_rules($path)

$rules is now something like

index.php?team=john-smith&post_type=team&name=john-smith;

or alternatively, the actual rewrite rules array (and I can build the string myself…)

array("team" => "john-smith", "post_type" => "team", "name"=> "john-smith");

Related posts

Leave a Reply

1 comment

  1. Have you looked at this function in includes/rewrite.php:

    function url_to_postid($url)

    My guess is that to get the actual rewrite rules, you would use:

    global $wp_rewrite;
    $rewrite = $wp_rewrite->wp_rewrite_rules();
    

    Or call page_rewrite_rules(). There’s a few interesting looking bits of code in that file.