Skip to content

Instantly share code, notes, and snippets.

@tw2113
Last active January 13, 2024 03:07
Show Gist options
  • Save tw2113/6920154 to your computer and use it in GitHub Desktop.
Save tw2113/6920154 to your computer and use it in GitHub Desktop.
Taxonomy Terms in Permalink
<?php
/*
Plugin Name: Enhance Insurance - Taxonomy Term In Permalink
Plugin URI: http://www.webdevstudios.com
Description: Add CPT taxonomy terms to permalink
Version: 1.0.0
Author: WebDevStudios
Author URI: http://www.webdevstudios.com
License: GPLv2
Text Domain: enhanceins
*/
add_action( 'init', 'enhanceins_tax_to_permalink' );
function enhanceins_tax_to_permalink() {
$enhanceins_taxonomy_permalinks = new Add_Taxonomy_To_Post_Permalinks( array( 'states', 'insurance-levels' ) );
}
add_rewrite_rule( "([^/]+)/car-insurance-([^/]+)/?$", 'index.php?post_type=YOURCPT&state=$matches[1]&city=$matches[2]&vertical=car-insurance', 'top' );
/**
* A helper class for registering and handling a custom rewrite tag for a custom taxonomy. Modified version from Viper0007Bond
*
* @version 1.0.0
*/
class Add_Taxonomy_To_Post_Permalinks {
/**
* Stores the taxonomy slug that this class will be handling. Don't edit this.
*
* @since 1.0.0
* @var array
*/
public $taxonomy_first;
/**
* Stores the taxonomy slug that this class will be handling. Don't edit this.
*
* @since 1.0.0
* @var array
*/
public $taxonomy_second;
/**
* Stores the rewrite tag complete with percentage signs. Don't edit this.
*
* @since 1.0.0
* @var string
*/
public $rewrite_tag_first;
/**
* Stores the rewrite tag complete with percentage signs. Don't edit this.
*
* @since 1.0.0
* @var string
*/
public $rewrite_tag_second;
/**
* Initializes the class by calling Add_Taxonomy_To_Post_Permalinks::register()
* as well as registering a filter that runs in get_permalink().
*
* @since 1.0.0
*
* @param string $taxonomy A taxonomy slug. Use the same one that you used with register_taxonomy().
* @return array $optional_args Optional configuration parameters. See Add_Taxonomy_To_Post_Permalinks::register().
*/
function __construct( $taxonomy, $optional_args = array() ) {
if ( ! $this->register( $taxonomy, $optional_args ) )
return;
// Normal posts
add_filter( 'post_link', array( &$this, 'filter_post_link' ), 10, 2 );
// Custom post types
add_filter( 'post_type_link', array( &$this, 'filter_post_link' ), 10, 2 );
}
/**
* Registers the rewrite tag using add_rewrite_tag().
*
* Can accept an array of optional parameters:
*
* * tagname: The rewrite tag to use (no percentage signs). Defaults to the taxonomy slug.
* * regex: What regex to use to validate the value of the tag. Defaults to anything but a forward slash.
*
* @since 1.0.0
*
* @param string $taxonomy A taxonomy slug. Use the same one that you used with register_taxonomy().
* @return array $optional_args Optional configuration parameters. See function description.
*/
public function register( $taxonomy, $optional_args = array() ) {
if ( !is_array( $taxonomy ) ) {
return false;
}
foreach( $taxonomy as $tax ) {
if ( ! taxonomy_exists( $tax ) )
return false;
}
$this->taxonomy_first = $taxonomy[0];
$this->taxonomy_second = $taxonomy[1];
$this->rewrite_tag_first = ( ! empty( $optional_args['tagname'] ) ) ? $optional_args['tagname'] : $this->taxonomy_first;
$this->rewrite_tag_first = '%' . $this->rewrite_tag_first . '%';
$this->rewrite_tag_second = ( ! empty( $optional_args['tagname'] ) ) ? $optional_args['tagname'] : $this->taxonomy_second;
$this->rewrite_tag_second = '%' . $this->rewrite_tag_second . '%';
$rewrite_tag_regex = ( ! empty( $optional_args['regex'] ) ) ? $optional_args['regex'] : '([^/]+)';
// See http://codex.wordpress.org/Rewrite_API/add_rewrite_tag
add_rewrite_tag( $this->rewrite_tag_first, $rewrite_tag_regex );
add_rewrite_tag( $this->rewrite_tag_second, $rewrite_tag_regex );
return true;
}
/**
* Filters a post permalink to replace the tag placeholder with the first
* used term from the taxonomy in question.
*
* @since 1.0.0
*
* @param string $permalink The existing permalink URL.
* @return object $post The post object that this permalink belongs to.
*/
public function filter_post_link( $permalink, $post ) {
//TODO: find a way to limit to just city_vertical post type
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, $this->rewrite_tag_first ) && false === strpos( $permalink, $this->rewrite_tag_second ) )
return $permalink;
// Get the custom taxonomy terms in use by this post
$state = get_the_terms( $post->ID, $this->taxonomy_first );
$level = get_the_terms( $post->ID, $this->taxonomy_second );
// If no terms are assigned to this post, use the taxonomy slug instead (can't leave the placeholder there)
if ( empty( $state ) || empty( $level ) ) {
$permalink = str_replace( $this->rewrite_tag_first, $this->taxonomy_first, $permalink );
$permalink = str_replace( $this->rewrite_tag_second, $this->taxonomy_second, $permalink );
}
// Replace the placeholder rewrite tag with the first term's slug
else {
$first_term_first = array_shift( $state );
$first_term_second = array_shift( $level );
$permalink = str_replace( $this->rewrite_tag_first, $first_term_first->slug, $permalink );
$permalink = str_replace( $this->rewrite_tag_second, $first_term_second->slug, $permalink );
}
return $permalink;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment