Last active
June 6, 2020 22:09
-
-
Save maheshwaghmare/583b69a76f43dd31c4a3f27e0bb36436 to your computer and use it in GitHub Desktop.
WordPress custom post type permalink rewrite structure rule for taxonomies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP_Dev_Rewrites initial setup | |
* | |
* @since 1.0.0 | |
*/ | |
if( !class_exists('WP_Dev_Rewrites') ) { | |
class WP_Dev_Rewrites { | |
private static $instance; | |
private $post_types; | |
/** | |
* Initiator | |
*/ | |
public static function get_instance(){ | |
if ( ! isset( self::$instance ) ) { | |
self::$instance = new WP_Dev_Rewrites(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
$this->post_types = array( | |
// 'post-type-1' => '%taxonomy-1%/%taxonomy-2%', | |
// 'post-type-2' => '%another-taxonomy-1%', | |
); | |
add_filter( 'rewrite_rules_array', array( $this, 'rules' ) ); | |
add_filter( 'post_type_link', array( $this, 'filter_post_type_link' ), 10, 2 ); | |
add_filter( 'register_post_type_args', array( $this, 'post_type_args' ), 10, 2 ); | |
// add_filter( 'register_taxonomy_args', array( $this, 'taxonomy_args' ), 10, 3 ); | |
} | |
// function taxonomy_args( $args, $taxonomy, $object_type ) { | |
// foreach ($this->post_types as $post_type => $post_taxonomies) { | |
// $taxonomies = explode('/', $post_taxonomies ); | |
// foreach ($taxonomies as $taxonomy_key => $stored_taxonomy) { | |
// $plain_slug = str_replace('%', '', $stored_taxonomy); | |
// $args['rewrite'] = array( | |
// 'slug' => $post_type, | |
// 'with_front' => true | |
// ); | |
// } | |
// } | |
// return $args; | |
// } | |
/** | |
* Tell WordPress how to interpret our project URL structure | |
* | |
* @param array $rules Existing rewrite rules | |
* @return array | |
*/ | |
function rules( $rules ) { | |
$new = array(); | |
foreach ($this->post_types as $post_type => $value) { | |
$new[ $post_type . '/([^/]+)/(.+)/?$'] = 'index.php?'.$post_type.'=$matches[2]'; | |
} | |
return array_merge( $new, $rules ); // Ensure our rules come first | |
} | |
/** | |
* Handle the '%taxonomy%' URL placeholder | |
* | |
* @param str $link The link to the post | |
* @param WP_Post object $post The post object | |
* @return str | |
*/ | |
function filter_post_type_link( $link, $post ) { | |
foreach ($this->post_types as $key => $post_taxonomies) { | |
$taxonomies = explode('/', $post_taxonomies ); | |
foreach ($taxonomies as $taxonomy_key => $taxonomy) { | |
$taxonomy_slug = str_replace('%', '', $taxonomy); | |
$term = get_the_terms( $post->ID, $taxonomy_slug ); | |
if ( $term ) { | |
$link = str_replace( $taxonomy, current( $term )->slug, $link ); | |
} | |
} | |
} | |
return $link; | |
} | |
function post_type_args( $args, $post_type ) { | |
if( isset( $this->post_types[ $post_type ] ) && ! empty( $this->post_types[ $post_type ] ) ) { | |
$args['rewrite'] = array( | |
'slug' => $post_type . '/' . $this->post_types[ $post_type ], | |
'with_front' => true | |
); | |
} | |
return $args; | |
} | |
} | |
/** | |
* Kicking this off by calling 'get_instance()' method | |
*/ | |
WP_Dev_Rewrites::get_instance(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment