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

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

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

1
2
3
4
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

Few words before we start

Welcome to Pro Developer! I am going to share all my knowledge and experience with you. Also if you have something you want to share with the world, we can publish it here.

You will learn from me and i will learn from you. I am sure that together we can create beautiful development web site with essential resources.

We are going to talk about PHP, AJAX, JavaScript, C#, .NET, Database modeling, Designing and Developing Applications, useful project management tools and who knows what. We will talk about OOP and Programming generally.

I developed a lot of WordPress plugins, so we can talk about WordPress development and customization. WP is powerful CMS, as you can see I am using it to run this web site. I don’t want to talk about me i want to talk about development.

This was enough for start – take care developers!