Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Use a spacebar or arrow keys to navigate

WP_Rewrite Isn't As Scary As You Think

Erick Hitter

Team Custom Lead @ Automattic

@ethitter

About Me

What Does WP_Rewrite Give Us?

 

Pretty permalinks!

 

 

http://ethitter.com/2013/08/year-in-review/

vs

http://ethitter.com/?p=3886

But How?

Magic!

But How?

 

Okay, it's not really magic. Sorry to disappoint.

 

Instead, it's all about Regular Expressions.

But How?

WordPress uses the chosen Permalink structure to build Regular Expressions that translate a pretty permalink to arguments for WP_Query.

 

/2013/08/year-in-review/ becomes:

array(
    'year'  => '2013',
    'month' => '08',
    'name'  => 'year-in-review',
)

Where Are These Regular Expressions?

 

In the database, specifically the options table.

 

The array of rewrite rules are stored in an option named rewrite_rules.

 

 

Rewrite Rule?!? Regular Expression?!?

 

"([0-9]{4})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$"

=>

"year=$matches[1]&monthnum=$matches[2]
    &name=$matches[3]&page=$matches[4]"

Creating Your Own

 

You're in luck, WP provides lots of options!

 

 

Not an exhaustive list

Add a new rule: add_rewrite_rule()

 

add_rewrite_rule( $regex, $redirect, $position )

 

add_rewrite_rule(
    "([0-9]{4})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$",
    "year=$matches[1]&monthnum=$matches[2]
        &name=$matches[3]&page=$matches[4]",
    "top"
)

Add a new endpoint: add_rewrite_endpoint()

 

add_rewrite_endpoint( $endpoint )

 

add_rewrite_endpoint( 'feed' )

 

add_rewrite_endpoint( 'print' )

Modify WP_Rewrite behaviour

 

Use the generate_rewrite_rules action.

 

Modify rewrite rules

 

Use the rewrite_rules_array filter.

 

Resources

 

Questions?

 

Slides are available at http://www.ethitter.com/blog/.

Check Twitter for a link as well: @ethitter.