PHP code example of humanmade / hm-rewrite

1. Go to this page and download the library: Download humanmade/hm-rewrite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

humanmade / hm-rewrite example snippets


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;
  }
) );

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/',
      ),
    );
  }
) );