Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/0f76af40254747c7af20 to your computer and use it in GitHub Desktop.
Save trepmal/0f76af40254747c7af20 to your computer and use it in GitHub Desktop.
<?php
$basic_rewrite = new Basic_Rewrite();
class Basic_Rewrite {
function __construct() {
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules_array' ) );
add_filter( 'query_vars', array( $this, 'query_vars' ) );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
}
// flush_rules() if our rules are not yet included
function wp_loaded() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['blog$'] ) || ! isset( $rules['blog/page/?([0-9]{1,})/?$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function rewrite_rules_array( $rules ) {
$newrules = array();
$newrules['blog$'] = 'index.php?bloghome';
$newrules['blog/page/?([0-9]{1,})/?$'] = 'index.php?bloghome&paged=$matches[1]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function query_vars( $vars ) {
array_push( $vars, 'bloghome' );
return $vars;
}
function pre_get_posts( $query ) {
if ( is_admin() ) return;
if ( ! isset( $query->query_vars['bloghome'] ) ) { return; }
$query->is_home = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment