How to filter custom permalinks & redirect array?

Permalink Manager has the ability to dynamically alter custom permalinks, which is one of its main features. If you would like to integrate your code with the plugin, the information presented in this article may be useful to you.

Custom URIs, redirects, permastructures, and plugin settings are serialized and saved in the wp_options database table in order to improve the performance of the website. This storage architecture is suitable for the majority of user scenarios, however it may not be effective if your website has thousands of posts or pages. Please see this post for further information about the plugin’s performance.

Custom permalinks and redirects can be accessed as global variables to make advantage of the unique algorithm that allows Permalink Manager to detect content element when a particular URL is requested.

global $permalink_manager_uris, $permalink_manager_redirects;

If necessary, you may preview all of the data by using the “Debug” section.

"Debug" section

How to process individual permalinks?
Individual URIs may also be retrieved and programmematically set using functions included in the Permalink Manager codebase. For additional information on this, please see the following article.

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

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);
}
Go up