https://github.com/humanmade/hm-rewrite
HM_Rewrite is a wrapper for the WordPress WP Rewrite system.
https://github.com/humanmade/hm-rewrite
Last synced: about 1 year ago
JSON representation
HM_Rewrite is a wrapper for the WordPress WP Rewrite system.
- Host: GitHub
- URL: https://github.com/humanmade/hm-rewrite
- Owner: humanmade
- Created: 2013-02-13T18:41:30.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2023-06-26T15:58:02.000Z (about 3 years ago)
- Last Synced: 2025-06-22T17:09:59.431Z (about 1 year ago)
- Language: PHP
- Homepage: http://hmn.md/wordpress-rewrite-rules-hm-core-style/
- Size: 46.9 KB
- Stars: 161
- Watchers: 28
- Forks: 20
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
hm-rewrite
==========
`HM_Rewrite` and `HM_Rewrite_Rule` are wrappers for the WordPress rewrite / wp_query system.
The goal of HM_Rewrite and associated fuctions / classes is to make it very easy to add new routing points with new pages (as in dynamic pages, `post_type_archive` etc). It basically wraps a few tasks into a nice API. Everything (almost) you need for setting up a new routing page can be done all at once, relying heavily on PHP Closures. It essentially wraps adding to the `rewrite_rules`, adding your template file to `template_redirect`, `wp_title` hook, `body_class` hook, `parse_query` hook etc. Also also provides some callbacks for conveniance. Each rewrite rule is an instance of `HM_Rewrite_Rule`. Here you add the regex / `wp_query` vars and any other options for the "page". For example a callback function to `parse_request` to add additional query vars, or a callback * `body_class`. There is also a wrapper function for all of this in one call `hm_add_rewrite_rule()`. `hm_add_rewrite_rule()` is generally the recommended interface, you can interact with the underlying objects for more advanced stuff (and also tacking onto other rewrite rules)Simple use case example:
```php
hm_add_rewrite_rule( array(
'regex' => '^users/([^/]+)/?',
'query' => 'author_name=$matches[1]',
'template' => 'user-archive.php',
'body_class_callback' => function( $classes ) {
$classes[] = 'user-archive';
$classes[] = 'user-' . get_query_var( 'author_name' );
return $classes;
},
'title_callback' => function( $title, $seperator ) {
return get_query_var( 'author_name' ) . ' ' . $seperator . ' ' . $title;
}
) );
```
A more advanced example using more callbacks:
```php
hm_add_rewrite_rule( array(
'regex' => '^reviews/([^/]+)/?', // a review category page
'query' => 'review_category=$matches[1]',
'template' => 'review-category.php',
'request_callback' => function( WP $wp ) {
// if the review category is "laptops" then only show items in draft
if ( $wp->query_vars['review_category'] == 'laptops' )
$wp->query_vars['post_status'] = 'draft';
},
'query_callback' => function( WP_Query $query ) {
//overwrite is_home because WordPress gets it wrong here
$query->is_home = false;
},
'body_class_callback' => function( $classes ) {
$classes[] = get_query_var( 'review_category' );
return $classes;
},
'title_callback' => function( $title, $seperator ) {
return review_category . ' ' . $seperator . ' ' . $title;
},
'rewrite_tests_callback' => function() {
return array(
'Review Category' => array(
'/reviews/foo/',
'/reviews/bar/',
),
);
}
) );
```
## Contribution guidelines ##
see https://github.com/humanmade/hm-rewrite/blob/master/CONTRIBUTING.md