Create your own rewrite rules in WordPress

When you write your own WordPress plugin you might need to add new rewrite rules to WordPress. Some people add Rewrite rules direct into htaccess file, but when you open it you can see that WP don’t store rules into this file. All WordPress rewrite rules are stored into the database.

Default WordPress htaccess file


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

For manipulation with rewrite you can use WP_Rewrite class. It’s functions all listed at the WP Codes, but i will show you how to use most important ones.

rewrite_rules() function

function getRewriteRules() {
    global $wp_rewrite; // Global WP_Rewrite class object
    return=$wp_rewrite->rewrite_rules(); 
}

This function “getRewriteRules”, will return array with listed rewrite rules. Array will be something like …

Array
(
    // ....
    [author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?author_name=$1&feed=$2
    [author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?author_name=$1&feed=$2
    [author/([^/]+)/page/?([0-9]{1,})/?$] => index.php?author_name=$1&paged=$2
    [author/([^/]+)/?$] => index.php?author_name=$1
    // ...
)

Continue reading