Hook to change Custom Tag Taxonomy Links?

I have a Custom Post Type named News that has a URL structure like this…

domain.com/news/news-post-name

Read More

I have also added a Custom Tag Taxonomy for my News Post Type.

Here is my Re-write settings for it…

$news_posts_tag_args = array(
        'rewrite' => array(
                'slug'                       => 'news-tag',
                'with_front'                 => false,
                'hierarchical'               => true,
        )
);

This makes mt News Tag have a URL like this…

domain.com/news-tag/news-TAG-name

However I would like my it to appear that I have tag under my News Post Type

So that it will instead be like this…

domain.com/news/tag/news-TAG-name

I am able to achieve this by using the Re-write code below…

function custom_taxonomies_rewrite(){
    add_rewrite_rule('^news/tag/([^/]*)/?','index.php?news_tag=$matches[1]','top');
}
add_action('init','custom_taxonomies_rewrite');

So far everything works great but I then have to make sure to edit anything that links to the News Tag Taxonomy to make sure it uses my new custom URL structure for that Taxonomy.

So I am hoping someone can help me with the next step of my process by helping to to hook into Tag links so maybe I can change the links more easily?

Related posts

Leave a Reply

1 comment

  1. If anyone else is looking for this in the future, the result I came up with is this one…

    /* Filter Custom Taxonomy Tag HTML Links */
    function custom_term_link_url($content) {
        $current_path = 'news-tag';
        $new_path = 'news/tag';
        $content = str_replace($current_path, $new_path, $content);
        return $content;
    }
    add_filter('term_link', 'custom_term_link_url');
    

    It will run code on Taxonomy Links. I still need to add in functionality to work with more then 1 custom taxonomy but this is a good start and works for 1