1. Home
  2. Docs
  3. Filters & Hooks
  4. Conditional permalinks for categories, tags or custom taxonomy terms

Conditional permalinks for categories, tags or custom taxonomy terms

Besides post permalinks Permalink Manager Pro allows to filter also default custom permalinks of categories, tags and custom taxonomies terms. Below you can find the description of permalink_manager_filter_default_term_uri and sample code snippet.

Please note that it is only a basic example. You can fully customize the function and additional parameters or conditions to the term’s custom permalinks. This includes eg. custom fields (meta keys), parent terms, languages and so on.

This feature is available only in Permalink Manager Pro

Just like with posts, you can use Permalink Manager to adjust the default custom permalinks with

permalink_manager_filter_default_term_uri($default_uri, $native_slug, $term, $slug, $native_uri)

filter. As you can see, along with the hook 5 arguments can be accessed with:

  • $default_uri is a final URI applied to the new terms or the old terms (after the permalinks are regenerated/reset)
  • $native_slug is a native, unfiltered term’s slug (it may differ from slug defined with $slug variable if “Force custom slugs” is enabled in Permalink Manager settings)
  • $term is a term object, an instance of WP_Term class, see more info here
  • $slug is a slug used normally by Permalink Manager
  • $native_uri, you should check in your custom code snippet if this variable is set to false (see examples below)

Dynamic/conditional permalinks for categories, tags or custom taxonomies

Analogous to the previous example, you can use different permalink formats/structures also for custom post types (eg. WooCommerce products) assigned to custom taxonomies.

Below you can find a basic snippet showing how WooCommerce conditional permalinks can be set-up to change the default products web addresses according to their product categories (and eg. custom fields in the first case).

function pm_filter_default_term_uris($default_uri, $native_slug, $term, $slug, $native_uri) {
	// Do not change native permalinks or different taxonomies
	if($native_uri || $term->taxonomy != 'product_cat') { return $default_uri; }

	// Get state name (stored as a term meta)
	$state_name = sanitize_title(get_term_meta($term->term_id, 'state', true));

	// Get city name (stored as a term meta)
	$city_name = sanitize_title(get_term_meta($term->term_id, 'city', true));

	// Get parent term
	$parent = ($term->parent !== 0) ? get_term($term->parent, $term->taxonomy) : "";

	// 1A. Cities permalinks filtered by state name (term meta) - East Coast
	// Example URL: http://shop.com/directory/east-coast/fl/miami/car-dealers
	if(!empty($state_name) && in_array($state_name, array('FL', 'GA', 'SC', 'NC', 'VA'))) {
		$default_uri =  "directory/east-coast/{$state_name}/{$city_name}/{$slug}";
	}

	// 1B. Cities permalinks filtered by state name (term meta) - West Coast
	// Example URL: http://shop.com/directory/east-coast/fl/miami/car-dealers
	else if(!empty($state_name) && in_array($state_name, array('WA', 'OR', 'CA'))) {
		$default_uri =  "directory/west-coast/{$state_name}/{$city_name}/{$slug}";
	}

	// 2. Other terms
	// Example URL: http://shop.com/shop/liquorice-chocolate/
	else {
		$default_uri =  "directory/{$slug}";
	}

	return trim($default_uri, "/");
}
add_filter('permalink_manager_filter_default_term_uri', 'pm_filter_default_term_uris', 10, 5);
Was this article helpful to you? Yes No