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

How to programmatically change the custom redirect array?

To filter the custom redirect ($permalink_manager_redirects) array for a single post or term you should use permalink-manager-redirects filter.

Also in this case, the custom redirects used by categories, tags or custom terms are stored in $permalink_manager_uris array with “tax-” prefix, eg.:

Array (
	[10] => Array (
		[0] => first-custom-redirect/asigned-to-a-single-post
		[1] => second-custom-redirect/asigned-to-a-single-post
		[2] => third-custom-redirect/asigned-to-a-single-post
	)
	[12] => Array (
		[0] => different-first-custom-redirect/asigned-to-another-single-post
		[1] => different-second-custom-redirect/asigned-to-another-single-post
		[2] => different-third-custom-redirect/asigned-to-another-single-post
	)
	...
	[tax-20] => Array (
		[0] => first-custom-redirect/asigned-to-a-single-term
		[1] => second-custom-redirect/asigned-to-a-single-term
		[2] => third-custom-redirect/asigned-to-a-single-term
	)
	[tax-28] => Array (
		[0] => different-first-custom-redirect/asigned-to-another-single-category
		[1] => different-second-custom-redirect/asigned-to-another-single-category
		[2] => different-third-custom-redirect/asigned-to-another-single-category
	)
)

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

function pm_filter_custom_redirects($custom_redirects) {
	$custom_redirects = (!is_array($custom_redirects)) ? array() : $custom_redirects;

	// Filter the redirects assigned to post/page/custom post item with id #99
	$custom_redirects["99"] = array("dynamically/added/custom-redirect", "another-dynamically/added/custom-redirect", "one-more/dynamically/added/custom-redirect");

	// Filter the redirects assigned to category/tag/custom term item with id #10
	// All terms should be prefixed with "tax-"
	$custom_redirects["tax-10"] = array("category/dynamically/added/custom-redirect", "category/another-dynamically/added/custom-redirect", "category/one-more/dynamically/added/custom-redirect");

	return $custom_redirects;
}
add_filter('permalink-manager-redirects', 'pm_filter_custom_redirects', 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: