How to filter the custom permalinks format?

Permalink Manager allows you to filter the default custom permalinks for both post types and taxonomies programmatically. This might be handy if you want the permalink format to be determined by a variable such as the assigned term, language, custom field, or another parameter

Permalink Manager, as opposed to the native permalink system, allows you greater control when it comes to rewriting your URL structures. The plugin’s filters may be highly useful if you want to set different/conditional permastructures based on the provided categories, post publishing date, author, title, or other criteria.

Some of the functionalities presented in this article are not available in free version (Permalink Manager Lite). Buy Permalink Manager Pro

You may programmatically change the default permalink formats by using the plugin hooks described below. Normally, the plugin uses the user-specified permalink formats provided in the “Permastructure” settings to generate new permalinks. If you want to make certain permalinks different when a specific condition is satisfied, just use the filters and adapt them to your requirements.

Basically, there are two methods to programmatically change the default custom permalinks. You may either filter the raw permastructure format that contains the tags such as %postname% or %category%. The alternative way is to change the final permalink after the permastructure tags are dynamically changed with distinct values for each post/term.

Keep in mind that your new, customized permalink formats will be used by default only for newly created content. The new programmatically updated formats may be applied to existing items when the permalinks are regenerated.

New permalink formats and existing content
The default permalinks mentioned in this article are used only for the new posts & terms permalinks. To apply the new permalink formats to existing items please use "Regenerate/reset" functionality.

The filter below may be handy if you wish to, for example, use a different URL structure for posts linked to certain categories. Similarly, instead of registering new post types or taxonomies only to give them alternative URLs depending on the circumstances, you may dynamically generate permalink bases.

A sample permastructure
Example permalink format (Tools -> Permalink Manager -> Permastructures)
permalink_manager_filter_permastructure($permastructure, $term_or_post)
Variable Description
$permastructure The permastructure string used when default custom permalink is generated (string)
Example input: store/%product_cat%/%product%
$term_or_post Post or term object (WP_Post|WP_Term)

The aforementioned filter may be used to dynamically update the generic permalink format, which is made up of specialized infrastructure tags (that are formatted in this manner: %postname%, %category%). When a post or term permalink is generated, the permastructure tags are dynamically replaced with actual data.

If necessary, you may update the default version of the custom permalink after the permastructure tags have been populated by using the filters mentioned below. One example of how this approach may be used is to automatically truncate the permalink if it becomes too lengthy.

In general, “default permalinks” are the individually processed version of generic permastructure formats. Only new posts and terms use them by default in normal circumstances. If you use the “Regenerate/reset” tool, they will also be used for existing items.

How can I programmatically modify post permalinks?

permalink_manager_filter_default_post_uri($default_uri, $native_slug, $post, $slug, $native_uri)
Variable Description
$default_uri Default custom peramlink based on your Permastructure settings (string)
Example input: https://example.com/men/clothes/shirts/jersey-cotton-shirt
$native_slug Native, unfiltered post’s slug. It may differ from slug defined with $slug variable if “Force custom slugs” is enabled in Permalink Manager settings (string)
$post Post object (WP_Post)
$slug Slug used normally by Permalink Manager to generate a custom permalink (string)
$native_uri To target only the custom permalinks and not the native ones, check if its value is set to “false” (boolean)

How can I programmatically modify term permalinks?

permalink_manager_filter_default_term_uri($default_uri, $native_slug, $term, $slug, $native_uri)
Variable Description
$default_uri Default custom peramlink based on your Permastructure settings (string)
Example input: https://example.com/women/clothes/dresses/
$native_slug Native, unfiltered post’s slug. It may differ from slug defined with $slug variable if “Force custom slugs” is enabled in Permalink Manager settings (string)
$term Term object (WP_Term)
$slug Slug used normally by Permalink Manager to generate a custom permalink (string)
$native_uri To target only the custom permalinks and not the native ones, check if its value is set to “false” (boolean)

Examples

Please keep in mind that the examples in this post have been purposefully simplified. You may totally modify the function’s code and adjust the parameters or conditions used inside it.

How to truncate the custom permalinks?

One of the most interesting uses of above hooks would be to shorten the length of the slugs. In particular, you may restrict the number of words in each slug that is the part of the custom permalink.

For example:

http://example.com/a-very-long-slug-with-seven-words/slug-with-exactly-five-words/another-pretty-long-sentence-that-should-be-shorten

will be shorten to:

http://example.com/a-very-long-slug-with/slug-with-exactly-five-words/another-pretty-long-sentence-that

The snippet shown in following example sets the maximal number of words to 5 but you can simply modify the limit in $max_words variable.

There are many compelling arguments for transliterating your WordPress permalinks. According to some experts, URL romanization may have a favorable impact on SEO and search engine rankings. Furthermore, international visitors will find it easier to browse via understandable URLs that only include Latin characters.

Although several WordPress plugins allow for the transliteration of WordPress slugs, most of them are either limited or no longer maintained. Using the Permalink Manager plugin, you can easily convert Greek, Bulgarian, Russian and Serbian Cyrillic letters to their Latin equivalents in your WordPress permalinks. To have the plugin transliterate URLs, you must use a custom code snippet.

Go up