1. Home
  2. Docs
  3. Filters & Hooks
  4. How the custom URIs and redirects are stored?

How the custom URIs and redirects are stored?

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;

How to programmatically change the custom URIs array

To dynamically filter the custom URI ($permalink_manager_uris) for a single post or term you should use 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
)

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

function pm_filter_custom_uris($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', 9);

The full array can be previewed in “Tools -> Permalink Manager -> Debug” section.

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

The full redirects array can be previewed in “Tools -> Permalink Manager -> Debug” section.

How to access the custom URIs or redirects array?

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