{"id":13549524,"url":"https://github.com/humanmade/hm-rewrite","last_synced_at":"2025-06-30T23:09:59.779Z","repository":{"id":6933839,"uuid":"8185155","full_name":"humanmade/hm-rewrite","owner":"humanmade","description":"HM_Rewrite is a wrapper for the WordPress WP Rewrite system.","archived":false,"fork":false,"pushed_at":"2023-06-26T15:58:02.000Z","size":48,"stargazers_count":161,"open_issues_count":8,"forks_count":20,"subscribers_count":28,"default_branch":"master","last_synced_at":"2025-06-22T17:09:59.431Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://hmn.md/wordpress-rewrite-rules-hm-core-style/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/humanmade.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2013-02-13T18:41:30.000Z","updated_at":"2025-03-14T03:58:10.000Z","dependencies_parsed_at":"2024-03-16T21:59:26.243Z","dependency_job_id":"c4f40091-82f3-4965-bf76-c274df8fd573","html_url":"https://github.com/humanmade/hm-rewrite","commit_stats":{"total_commits":44,"total_committers":15,"mean_commits":2.933333333333333,"dds":0.8181818181818181,"last_synced_commit":"1cf1a805438f9fea982076b1fb35580b09e228a1"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/humanmade/hm-rewrite","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-rewrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-rewrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-rewrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-rewrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/humanmade","download_url":"https://codeload.github.com/humanmade/hm-rewrite/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-rewrite/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261927548,"owners_count":23231374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:01:22.698Z","updated_at":"2025-06-30T23:09:59.746Z","avatar_url":"https://github.com/humanmade.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"hm-rewrite\n==========\n\n`HM_Rewrite` and `HM_Rewrite_Rule` are wrappers for the WordPress rewrite / wp_query system.\n\nThe 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:\n\n```php\nhm_add_rewrite_rule( array(\n  'regex'     =\u003e '^users/([^/]+)/?',\n  'query'     =\u003e 'author_name=$matches[1]',\n  'template'  =\u003e 'user-archive.php',\n  'body_class_callback' =\u003e function( $classes ) {\n    $classes[] = 'user-archive';\n    $classes[] = 'user-' . get_query_var( 'author_name' );\n\n    return $classes;\n  },\n  'title_callback' =\u003e function( $title, $seperator ) {\n    return get_query_var( 'author_name' ) . ' ' . $seperator . ' ' . $title;\n  }\n) );\n```\n\nA more advanced example using more callbacks:\n\n```php\nhm_add_rewrite_rule( array(\n  'regex'    =\u003e '^reviews/([^/]+)/?', // a review category page\n  'query'    =\u003e 'review_category=$matches[1]',\n  'template' =\u003e 'review-category.php',\n  'request_callback' =\u003e function( WP $wp ) {\n    // if the review category is \"laptops\" then only show items in draft\n    if ( $wp-\u003equery_vars['review_category'] == 'laptops' )\n      $wp-\u003equery_vars['post_status'] = 'draft';\n  },\n  'query_callback' =\u003e function( WP_Query $query ) {\n    //overwrite is_home because WordPress gets it wrong here\n    $query-\u003eis_home = false;\n  },\n  'body_class_callback' =\u003e function( $classes ) {\n    $classes[] = get_query_var( 'review_category' );\n    return $classes;\n  },\n  'title_callback' =\u003e function( $title, $seperator ) {\n    return review_category . ' ' . $seperator . ' ' . $title;\n  },\n  'rewrite_tests_callback' =\u003e function() {\n    return array(\n      'Review Category' =\u003e array(\n        '/reviews/foo/',\n        '/reviews/bar/',\n      ),\n    );\n  }\n) );\n```\n\n## Contribution guidelines ##\n\nsee https://github.com/humanmade/hm-rewrite/blob/master/CONTRIBUTING.md\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhumanmade%2Fhm-rewrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhumanmade%2Fhm-rewrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhumanmade%2Fhm-rewrite/lists"}