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.

In general, the plugin is designed specifically for small and medium-sized websites that use the WordPress platform. One of the key features of this plugin is its unique storage model. Instead of creating a separate entry in the database for each permalink, Permalink Manager stores all custom permalinks in a single array within the wp_options table.

This approach has a number of advantages. One of the most notable is that it reduces the number of slow SQL queries that need to be made to the database. Because the permalinks are stored in an array, the plugin can utilize the faster operations that are available in PHP when working with arrays. These operations are performed in server memory, rather than in the database, which can greatly improve performance.

Additionally, because the permalinks are stored in a single array, it is much easier to manage and organize them, which can be especially useful for dedicated applications.

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

Permalink Manager allows you to easily update and manage custom permalinks on your WordPress website. Each custom permalink is assigned to a single post or term (category) by their IDs and stored in an array. It supports two types of content: posts, pages & custom content types such as WooCommerce products AND post tags, categories, and other custom taxonomies such as product categories.

As previously mentioned, while the plugin allows you to change existing permalinks to almost anything you desire, it does have a few limitations.

  1. One of the primary limitations is that, given that all custom permalinks are stored statically in an array, you cannot define multiple canonical permalinks for the same page or term. This limitation is in place to decrease server resource consumption and improve processing time.

  2. Another limitation is that it is not possible to create custom permalinks for non-existent combinations of categories. For example, it is not possible to create a new archive page that would list all products that match BOTH product category: "shoes" and product tag: "leather" as such combination does not exist anywhere in the database.

    It's important to understand that the inability to create custom permalinks for combinations of categories is not a limitation of Permalink Manager, but rather a limitation of WordPress itself. There is no way to customize such a combination in the same manner that you can adjust individual categories and tags. Even the possibility of defining a title for such a combination is not available anywhere inside the WordPress dashboard.

    In other words, if an archive page (for example, a product category or term) already exists, you may alter its URL using Permalink Manager, but if it does not exist you cannot build it by combining different archive pages.

  3. Another limitation is that when a URL is opened and Permalink Manager parses it to determine which page it belongs to, the plugin ignores the $_GET variables (query parameters). As a result, you may not use the Permalink Manager plugin to rewrite the query parameters.

    This means that it is not possible to format URLs with query parameters in order to convert them into pretty permalinks. This is because, as previously stated, a page may only have one static custom permalink.

    Custom permalinks cannot be defined for dynamically generated URLs (with query parameters):

    https://shop.com/location/?state=new-jersey&city=newark

    https://shop.com/location/new-jersey/newark

    Additionally, it is not possible to construct a custom permalink format using Permastructures that include query parameters in the URL. Because of this, all non-alphanumeric characters, like question marks and ampersands, will be automatically removed from the custom permalink's (canonical URLs).

    In custom permalinks, you cannot use query parameters:

    https://shop.com/clothes/shirts?filter_material=wool&filter_color=blue

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