/**
 * Add rewrite for new query vars
 *
 * @return array
 */
function fd_add_rewrite_rules($aRules)
{
    $new1 = array('plant-group/(.+?)/genus/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?fdplt_plant_group=$matches[1]&fdplt_genus=$matches[2]&paged=$matches[3]');
    $new2 = array('plant-group/(.+?)/genus/(.+?)/?$' => 'index.php?fdplt_plant_group=$matches[1]&fdplt_genus=$matches[2]');
    $aRules = $new1 + $new2 + $aRules;

    return $aRules;
}

add_filter('rewrite_rules_array', 'fd_add_rewrite_rules');

Note: The rule with pagination needs to come first otherwise it will use the non-page rewrite rule.

Extensions for Testing

These extensions are helpful for testing during development of your rewrites.  Once you have completed development and everything is rewriting as desired these extensions can be removed.

Monkeyman Rewrite Analyzer

Monkeyman Rewrite Analyzer allows you to enter your proposed URL and it will highlight the related rewrite rule that will be used. Note: As of this writing, the plugin hasn’t been updated in a while, but it still works like a charm.

Rewrite Rules Inspector

Rewrite Rules Inspector shows you all of your current rewrite rules and lets your easily “Flush Rules” to pick up any of your new changes as you edit them in your code. Note: Rewrite rules are cached and only updated when a flush rules command is run.

Filtering on a Custom Variable

If you are using a new custom variable that is not already tracked by the build in WordPress query_vars you will need to add it.

/**
 * Tell WordPress to track a new query variables
 *
 * @return array
 */
public function fd_add_query_vars($aVars)
{
    $aVars[] = "my_custom_var";
    return $aVars;
}

add_filter('query_vars', 'fd_add_query_vars');
2.6 5 votes
Article Rating
in WordPress
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments