Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ihfbib/e8f87bba8268f38964b5e483ffe861e2 to your computer and use it in GitHub Desktop.

Select an option

Save ihfbib/e8f87bba8268f38964b5e483ffe861e2 to your computer and use it in GitHub Desktop.

Revisions

  1. @mattyza mattyza revised this gist Jan 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion matty-double-post-type-rewrite-rule-example.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    * 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-2.0.html
    * 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
    *
  2. @mattyza mattyza revised this gist Jan 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion matty-double-post-type-rewrite-rule-example.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    /**
    * Plugin Name: Double Post Type Rewrite Rule Example
    * Plugin URI: http://matty.co.za/wordpress/
    * 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/
  3. @mattyza mattyza created this gist Jan 4, 2015.
    106 changes: 106 additions & 0 deletions matty-double-post-type-rewrite-rule-example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    <?php
    /**
    * Plugin Name: Double Post Type Rewrite Rule Example
    * Plugin URI: http://matty.co.za/wordpress/
    * 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-2.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