1. Home
  2. Docs
  3. Filters & Hooks
  4. Filter custom permalinks & redirect array

Filter custom permalinks & redirect array

To optimize the performance, the custom URIs, redirects, permastructures and plugin settings are serialized and stored in wp_options table.

Both arrays are registered as globals and can be accessed and/or filtered by 3rd party plugins:

global $permalink_manager_uris, $permalink_manager_redirects;
The full arrays can be previewed in “Tools -> Permalink Manager -> Debug” section.

Custom permalinks

As you can see the indexes for post permalinks are integers (10, 12). The term permalinks’ indexes are prefixed with “tax-” (tax-20, tax-28):

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
)

One of the biggest advantages of Permalink Manager is a possibility to to dynamically manipulate the custom permalinks. To access the permalinks array you can use $permalink_manager_uris global.

function pm_filter_and_save_custom_uris() {
	global $permalink_manager_uris;

	// Set/change the custom permalink (Post ID: #99)
	$permalink_manager_uris[12] = "changed/custom-uri";

	// All terms permalinks should be prefixed with "tax-" (Term ID: 10)
	$permalink_manager_uris["tax-20"] = "filtered-custom-uri/assigned-to-a-single-term";
	
	// Save the array in DB
	update_option('permalink-manager-uris', $permalink_manager_uris);
}

You can also change the custom permalinks dynamically. The snippet below allows to directly filter the value of $permalink_manager_uris global before the permalinks array is loaded from the databse.

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

	// Change the post permalink (Post ID: #99)
	$custom_uris["99"] = "dynamically/changed/custom-uri";

	// Filter the permalink assigned to category/tag/custom term item with id #10
	$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);

Custom redirects

Analogous to custom permalinks, the custom redirects array contains groups of URIs assigned to posts and term by their IDs:

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
	)
	...
	[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 very basic example that shows how the custom redirects can be edited programmatically:

function pm_filter_and_save_custom_redirects() {
	global $permalink_manager_redirects;

	// Set new custom redirect (Post ID: #10)
	$permalink_manager_redirects[10][] = "new/custom-redirect"

	// Remove one of redirects (Term ID: 28)
	unset($permalink_manager_redirects["tax-28"][1]);
	
	// Save the array in DB
	update_option('permalink-manager-redirects', $permalink_manager_redirects);
}

It is possible to filter the custom redirects dynamically before $permalink_manager_redirects global is set:

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);

How to access a single custom URI?

Below you can find a sample function that returns the custom URI (false if no custom URI is assigned to the post or term):

function pm_get_custom_uri($id, $is_term = false) {
	global $permalink_manager_uris;

	$permalink_manager_uris = (!is_array($permalink_manager_uris)) ? array() : $permalink_manager_uris;

	// Get the custom URI assigned to selected post
	if(!$is_term && !empty($permalink_manager_uris[$id])) {
		$custom_uri = $permalink_manager_uris[$id];
	} else if(!empty($permalink_manager_uris["tax-{$id}"])) {
		$custom_uri = $permalink_manager_uris["tax-{$id}"];
	} else {
		$custom_uri = false;
	}

	return $custom_uri;
}

// Get the custom URI assigned to post with ID #82
$custom_uri = pm_get_custom_uri(82);

// Get the custom URI assigned to term with ID #54
$custom_uri = pm_get_custom_uri(54, true);