Title: wp_rewrite
Last modified: August 19, 2016

---

# wp_rewrite

 *  [jeremib](https://wordpress.org/support/users/jeremib/)
 * (@jeremib)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/wp_rewrite/)
 * I’m having the hardest time getting my plugin rewrite rules to work. Here’s my
   hook:
 *     ```
       public function __construct() {
          add_filter('generate_rewrite_rules', array($this, 'createRewriteRules'));
         ...
       }
       public function createRewriteRules($wp_rewrite) {
       	  $tag = 'shop';
   
           $newrules = array(
                               "{$tag}/(.+?)" => 'index.php?pagename=shopping&q=' . $wp_rewrite->preg_index(1),
                             ); 
   
           $wp_rewrite->rules =  $wp_rewrite->rules + $newrules;
           return $wp_rewrite;
       	}
       ```
   
 * When I print the wp_rewrite rules right before I return it above, I get:
 * > Array
   >  ( [robots\.txt$] => index.php?robots=1 [.*wp-atom.php$] => index.php?
   > feed=atom [.*wp-rdf.php$] => index.php?feed=rdf [.*wp-rss.php$] => index.php?
   > feed=rss [.*wp-rss2.php$] => index.php?feed=rss2 [.*wp-feed.php$] => index.
   > php?feed=feed [.*wp-commentsrss2.php$] => index.php?feed=rss2&withcomments=
   > 1 [feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?&feed=$matches[1] [(feed|
   > rdf|rss|rss2|atom)/?$] => index.php?&feed=$matches[1] [page/?([0-9]{1,})/?$]
   > => index.php?&paged=$matches[1] [comments/feed/(feed|rdf|rss|rss2|atom)/?$]
   > => index.php?&feed=$matches[1]&withcomments=1 [comments/(feed|rdf|rss|rss2|
   > atom)/?$] => index.php?&feed=$matches[1]&withcomments=1 [comments/page/?([0-
   > 9]{1,})/?$] => index.php?&paged=$matches[1] [search/(.+)/feed/(feed|rdf|rss
   > |rss2|atom)/?$] => index.php?s=$matches[1]&feed=$matches[2] [search/(.+)/(feed
   > |rdf|rss|rss2|atom)/?$] => index.php?s=$matches[1]&feed=$matches[2] [search/(.
   > +)/page/?([0-9]{1,})/?$] => index.php?s=$matches[1]&paged=$matches[2] [search/(.
   > +)/?$] => index.php?s=$matches[1] [category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?
   > $] => index.php?category_name=$matches[1]&feed=$matches[2] [category/(.+?)/(
   > feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[
   > 2] [category/(.+?)/page/?([0-9]{1,})/?$] => index.php?category_name=$matches[
   > 1]&paged=$matches[2] [category/(.+?)/?$] => index.php?category_name=$matches[
   > 1] [tag/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?tag=$matches[1]&
   > feed=$matches[2] [tag/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?tag=$
   > matches[1]&feed=$matches[2] [tag/(.+?)/page/?([0-9]{1,})/?$] => index.php?tag
   > =$matches[1]&paged=$matches[2] [tag/(.+?)/?$] => index.php?tag=$matches[1] [
   > author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?author_name=$
   > matches[1]&feed=$matches[2] [author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] =>
   > index.php?author_name=$matches[1]&feed=$matches[2] [author/([^/]+)/page/?([
   > 0-9]{1,})/?$] => index.php?author_name=$matches[1]&paged=$matches[2] [author/([
   > ^/]+)/?$] => index.php?author_name=$matches[1] [([0-9]{4})/([0-9]{1,2})/([0-
   > 9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[1]&monthnum
   > =$matches[2]&day=$matches[3]&feed=$matches[4] [([0-9]{4})/([0-9]{1,2})/([0-
   > 9]{1,2})/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[1]&monthnum
   > =$matches[2]&day=$matches[3]&feed=$matches[4] [([0-9]{4})/([0-9]{1,2})/([0-
   > 9]{1,2})/page/?([0-9]{1,})/?$] => index.php?year=$matches[1]&monthnum=$matches[
   > 2]&day=$matches[3]&paged=$matches[4] [([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?
   > $] => index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3] [([0-
   > 9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[
   > 1]&monthnum=$matches[2]&feed=$matches[3] [([0-9]{4})/([0-9]{1,2})/(feed|rdf
   > |rss|rss2|atom)/?$] => index.php?year=$matches[1]&monthnum=$matches[2]&feed
   > =$matches[3] [([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$] => index.php?year
   > =$matches[1]&monthnum=$matches[2]&paged=$matches[3] [([0-9]{4})/([0-9]{1,2})/?
   > $] => index.php?year=$matches[1]&monthnum=$matches[2] [([0-9]{4})/feed/(feed
   > |rdf|rss|rss2|atom)/?$] => index.php?year=$matches[1]&feed=$matches[2] [([0-
   > 9]{4})/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[1]&feed=$matches[
   > 2] [([0-9]{4})/page/?([0-9]{1,})/?$] => index.php?year=$matches[1]&paged=$matches[
   > 2] [([0-9]{4})/?$] => index.php?year=$matches[1] [[0-9]{4}/[0-9]{1,2}/[^/]+/
   > attachment/([^/]+)/?$] => index.php?attachment=$matches[1] [[0-9]{4}/[0-9]{
   > 1,2}/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[
   > 1]&tb=1 [[0-9]{4}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2
   > |atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2] [[0-9]{4}/[
   > 0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?
   > attachment=$matches[1]&feed=$matches[2] [[0-9]{4}/[0-9]{1,2}/[^/]+/attachment/([
   > ^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage
   > =$matches[2] [([0-9]{4})/([0-9]{1,2})/([^/]+)/trackback/?$] => index.php?year
   > =$matches[1]&monthnum=$matches[2]&name=$matches[3]&tb=1 [([0-9]{4})/([0-9]{
   > 1,2})/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[
   > 1]&monthnum=$matches[2]&name=$matches[3]&feed=$matches[4] [([0-9]{4})/([0-9]{
   > 1,2})/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?year=$matches[1]&monthnum
   > =$matches[2]&name=$matches[3]&feed=$matches[4] [([0-9]{4})/([0-9]{1,2})/([^/]
   > +)/page/?([0-9]{1,})/?$] => index.php?year=$matches[1]&monthnum=$matches[2]&
   > name=$matches[3]&paged=$matches[4] [([0-9]{4})/([0-9]{1,2})/([^/]+)/comment-
   > page-([0-9]{1,})/?$] => index.php?year=$matches[1]&monthnum=$matches[2]&name
   > =$matches[3]&cpage=$matches[4] [([0-9]{4})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?
   > $] => index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]&page
   > =$matches[4] [[0-9]{4}/[0-9]{1,2}/[^/]+/([^/]+)/?$] => index.php?attachment
   > =$matches[1] [[0-9]{4}/[0-9]{1,2}/[^/]+/([^/]+)/trackback/?$] => index.php?
   > attachment=$matches[1]&tb=1 [[0-9]{4}/[0-9]{1,2}/[^/]+/([^/]+)/feed/(feed|rdf
   > |rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2] [[
   > 0-9]{4}/[0-9]{1,2}/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?
   > attachment=$matches[1]&feed=$matches[2] [[0-9]{4}/[0-9]{1,2}/[^/]+/([^/]+)/
   > comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[
   > 2] [([0-9]{4})/([0-9]{1,2})/comment-page-([0-9]{1,})/?$] => index.php?year=
   > $matches[1]&monthnum=$matches[2]&cpage=$matches[3] [([0-9]{4})/comment-page-([
   > 0-9]{1,})/?$] => index.php?year=$matches[1]&cpage=$matches[2] [.+?/attachment/([
   > ^/]+)/?$] => index.php?attachment=$matches[1] [.+?/attachment/([^/]+)/trackback/?
   > $] => index.php?attachment=$matches[1]&tb=1 [.+?/attachment/([^/]+)/feed/(feed
   > |rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2][.
   > +?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=
   > $matches[1]&feed=$matches[2] [.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?
   > $] => index.php?attachment=$matches[1]&cpage=$matches[2] [(.+?)/trackback/?
   > $] => index.php?pagename=$matches[1]&tb=1 [(.+?)/feed/(feed|rdf|rss|rss2|atom)/?
   > $] => index.php?pagename=$matches[1]&feed=$matches[2] [(.+?)/(feed|rdf|rss|
   > rss2|atom)/?$] => index.php?pagename=$matches[1]&feed=$matches[2] [(.+?)/page/?([
   > 0-9]{1,})/?$] => index.php?pagename=$matches[1]&paged=$matches[2] [(.+?)/comment-
   > page-([0-9]{1,})/?$] => index.php?pagename=$matches[1]&cpage=$matches[2] [(.
   > +?)(/[0-9]+)?/?$] => index.php?pagename=$matches[1]&page=$matches[2] [shop/(.
   > +?)] => index.php?pagename=shopping&q=$matches[1] )
 * You can see the last element is what I added.
 * If I visit index.php?pagename=shopping&q=category/1234 everything works fine.
   though if I visit /shop/category/1234 I get page not found.
 * Any advice would be much appreciated. Thanks!

The topic ‘wp_rewrite’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [jeremib](https://wordpress.org/support/users/jeremib/)
 * Last activity: [16 years, 11 months ago](https://wordpress.org/support/topic/wp_rewrite/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
