1. Home
  2. Docs
  3. Filters & Hooks
  4. How to filter the default custom permalinks?

How to filter the default custom permalinks?

Permalink Manager allows to programmatically filter the default custom permalinks for both post types & taxonomies. This might be useful if you would like to make the permalink format dependent on eg. assigned term, language, custom field or another variable.

Please note that the default permalinks are used only for the new posts & terms, but you can easily apply the new filtered formats to the old items using “Regenerate/reset” functionality.

Some of the functionalities presented in this post are available only in Permalink Manager Pro.

Filter (custom) post types permalinks

permalink_manager_filter_default_post_uri($default_uri, $native_slug, $post, $slug, $native_uri)

This filter can be accessed with 5 arguments:

  • $default_uri
  • $native_slug is a native, unfiltered post’s slug ($post->post_name property)
  • $post is a post object, an instance of WP_Post class, see more info here
  • $slug is a post slug used in the custom permalinks (most probably, it will be appended to the end of custom permalink)
  • $native_uri, you should check in your custom code snippet if this variable is set to false (see examples below)
function pm_filter_default_post_uris($default_uri, $native_slug, $post, $slug, $native_uri) {
	// Do not change the native permalinks => Apply the filter only to the custom permalinks
if($native_uri) { return $default_uri; }

	// Get the year when the post was published
	$year = date("Y", strtotime($post->post_date));

	// Get post author meta - it can be user_login, user_email, ID or other field:
	// Reference: https://developer.wordpress.org/reference/functions/get_the_author_meta/
	$author = get_the_author_meta('user_login', $post->post_author);

	// A. Posts published in and after 2018 should contain the publication date
	// Example URL: http://example.com/news-and-resources/2019/hello-world/
	if(intval($year) >= 2018) {
		$default_uri =  "news-and-resources/{$year}/{$slug}";
	}

	// B. Posts published before 2018 should contain the author's slug
	// Example URL: http://example.com/archive/john-doe/nulla-imperdiet/
	else {
		$default_uri =  "archive/{$author}/{$slug}";
	}

	return trim($default_uri, "/");
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_filter_default_post_uris', 10, 5);

Filter (custom) taxonomy permalinks

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

This filter can be accessed with 5 arguments:

  • $default_uri
  • $native_slug is a native, unfiltered term’s slug ($term->slug property)
  • $post is a term object, an instance of WP_Term class, see more info here
  • $slug is a term slug used in the custom permalinks (most probably, it will be appended to the end of custom permalink)
  • $native_uri, you should check in your custom code snippet if this variable is set to false (see examples below)
function pm_filter_default_term_uris($default_uri, $native_slug, $term, $slug, $native_uri) {
	// Do not change the native permalinks => Apply the filter only to the custom permalinks
	if($native_uri) { return $default_uri; }

	// Get the parent term
	if(isset($term->parent)) {
		$parent_term = get_term_by('id', $term->parent, $term->taxonomy);
	}
	// Do not filter the default custom permalink if no parent term is set
	else {
		return $default_uri;
	}

	// A. Terms that are children of "shoes" category
	// Example URL: http://example.com/adidas-shoes/
	if($parent_term->slug == 'shoes') {
		$default_uri = "{$slug}-shoes";
	}

	// B. Terms that are children of "jackets" category
	// Example URL: http://example.com/outerwear/jackets/north-face
	else if($parent_term->slug == 'jackets') {
		$default_uri = "outerwear/{$parent_term->slug}/{$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