Skip to content

Instantly share code, notes, and snippets.

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 alexanderdejong/4dc0f2a0bf7e318343e34e2924ac8321 to your computer and use it in GitHub Desktop.
Save alexanderdejong/4dc0f2a0bf7e318343e34e2924ac8321 to your computer and use it in GitHub Desktop.
A WordPress custom rewrite rule example, combining two post types.
<?php
/**
* Plugin Name: Double Post Type Rewrite Rule Example
* Plugin URI: https://gist.github.com/mattyza/7dfd156992f23835f68e
* Description: Adds a custom rewrite rule to mimic http://domain.com/post-type-01/post-type-02/.
* Author: Matty Cohen
* Author URI: http://matty.co.za/
* Version: 1.0.0
* Stable tag: 1.0.0
* License: GPL v3 - http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
* Requires at least: 4.1.0
* Tested up to: 4.1.0
*
* Text Domain: matty-double-post-type-rewrite-rule-example
* Domain Path: /languages/
*/
new Matty_Double_Post_Type_Rewrite_Rule_Example();
class Matty_Double_Post_Type_Rewrite_Rule_Example {
private $primary_post_type_token;
private $secondary_post_type_token;
public function __construct () {
$this->primary_post_type_token = 'product';
$this->secondary_post_type_token = 'page_slug';
add_action( 'generate_rewrite_rules', array( $this, 'create_rewrite_rules' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
add_action( 'template_include', array( $this, 'process_rewrite_request' ) );
} // End __construct()
/**
* Generate our custom rewrite rules and add them to the array of registered rewrite rules within WordPress.
*
* @access public
* @since 1.0.0
* @return array
*/
public function create_rewrite_rules () {
global $wp_rewrite;
$primary_post_type_token_tag = '%primary_post_type_token%';
$secondary_post_type_token_tag = '%secondary_post_type_token%';
$wp_rewrite->add_rewrite_tag( $primary_post_type_token_tag, '(.+?)', 'post_type=product&' . $this->primary_post_type_token . '=' );
$wp_rewrite->add_rewrite_tag( $secondary_post_type_token_tag, '(.+?)', $this->secondary_post_type_token . '=' );
$rewrite_keywords_structure = $wp_rewrite->root . "entry/$secondary_post_type_token_tag/$primary_post_type_token_tag/";
$new_rule = $wp_rewrite->generate_rewrite_rules( $rewrite_keywords_structure );
$wp_rewrite->rules = $new_rule + $wp_rewrite->rules;
// To view the rewrite rules array, uncomment the line below and visit the Settings > Permalinks screen.
// echo '<xmp>'; print_r( $wp_rewrite->rules ); echo '</xmp>'; // DEBUG
return $wp_rewrite->rules;
} // End create_rewrite_rules()
/**
* Register our custom querystring variables within WordPress.
*
* @access public
* @since 1.0.0
* @param array $query_vars
* @return array $query_vars
*/
public function add_query_vars ( $query_vars ) {
$query_vars[] = $this->primary_post_type_token;
$query_vars[] = $this->secondary_post_type_token;
return $query_vars;
} // End add_query_vars()
/**
* Flush rewrite rules on init of WordPress. FOR DEVELOPMENT PURPOSES ONLY.
*
* @access public
* @since 1.0.0
* @return void
*/
public function flush_rewrite_rules () {
global $wp_rewrite;
$wp_rewrite->flush_rules();
} // End flush_rewrite_rules()
/**
* Process our rewrite rules request.
*
* @access public
* @since 1.0.0
* @param string $action
* @param string $type
* @param string $key
* @return array $response
*/
public function process_rewrite_request ( $tpl ) {
// To view the various query variables we've registered, uncomment the lines below and attempt to visit a URL using your custom rewrite rule.
// eg: http://domain.com/sample-page/flying-ninja/.
// echo '<xmp>'; print_r( get_query_var( 'product' ) ); echo '</xmp>'; // DEBUG
// echo '<xmp>'; print_r( get_query_var( 'page_slug' ) ); echo '</xmp>'; // DEBUG
// echo '<xmp>'; print_r( get_query_var( 'post_type' ) ); echo '</xmp>'; // DEBUG
return $tpl;
} // End process_rewrite_request()
} // End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment