1. Home
  2. Docs
  3. Filters & Hooks
  4. How to programmatically change the custom permalinks array?

How to programmatically change the custom permalinks array?

One of the biggest advantages of Permalink Manager is a possibility to to dynamically manipulate the custom permalinks (parsed as $permalink_manager_uris global) assigned to any WordPress post, page or term using permalink-manager-uris filter.

Please remember that the custom URIs used by categories, tags or custom terms are stored in $permalink_manager_uris array with “tax-” prefix, eg.:

Array (
	[10] => custom-uri/used-by-a-single-post
	[12] => another-custom-uri/used-by-another-single-post
	...
	[tax-20] => custom-uri/used-by-a-single-term-tag-or-category
	[tax-28] => another-custom-term-permalink-example
)

Modify custom permalinks

Below you can find a sample filter that allows to modify the custom URIs array:

function pm_filter_custom_uris() {
	global $permalink_manager_uris;

	// Filter the permalink assigned to post/page/custom post item with id #99
	$permalink_manager_uris["99"] = "changed/custom-uri";

	// Filter the permalink assigned to category/tag/custom term item with id #10
	// All terms should be prefixed with "tax-"
	$permalink_manager_uris["tax-10"] = "filtered-custom-uri/assigned-to-a-single-term";
	
	// Save the array
	update_option('permalink-manager-uris', $permalink_manager_uris);
}

Modify custom permalinks on fly

You can also change the custom permalinks on-fly if some or all of your post/term URLs should be completely dynamic. The snippet below allows to directly filter the value of $permalink_manager_uris global.

function pm_filter_custom_uris_on_fly($custom_uris) {
	$custom_uris = (!is_array($custom_uris)) ? array() : $custom_uris;

	// Filter the permalink assigned to post/page/custom post item with id #99
	$custom_uris["99"] = "dynamically/changed/custom-uri";

	// Filter the permalink assigned to category/tag/custom term item with id #10
	// All terms should be prefixed with "tax-"
	$custom_uris["tax-10"] = "filtered-custom-uri/assigned-to-a-single-term";

	return $custom_uris;
}
add_filter('permalink-manager-uris', 'pm_filter_custom_uris_on_fly', 9);

Was this article helpful to you? Yes No

How can we help?

Wait!!! Before you go...

Still not convinced? Use following discount code during Gumroad checkout and get 10% off!

BLOG

If you have any questions or need further information, please feel free to contact me via email: