Redirect /custom-taxonomy/123/gibberish to /123/canonical-slug

[ad_1]

Hello, I’m trying to add IDs to my custom terms permalinks. The idea is that instead of

/folders/cool-folder

I’d instead have

/folders/123/cool-folder

Where 123 is the term ID.

I managed to accomplish this with this code in `init`:

add_rewrite_tag(“%folder_id%”, ‘([0-9]+)’, “taxonomy=”.$taxonomy_name.”&tag_id=” );
add_rewrite_tag(“%$taxonomy_name%”, ‘([^/]+)’, “taxonomy=”.$taxonomy_name.”&term=” );
$rewrite_args = array();
$rewrite_struct = “folders/%folder_id%/%$taxonomy_name%”;
add_permastruct($taxonomy_name, $rewrite_struct, $rewrite_args);

**The problem:** I get a 404 error if the ID and slug don’t match exactly, e.g.

/folders/123/kool-foolder
/folders/123/asdasdad
/folders/123/
/folders/123

How can I redirect these to the canonical with ID and slug?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer