• By default, the Tag prefix will be shown in the URL.
    Any Tag will automatically have the tag prefix frontloaded, for example:
    https://yoururl.com/tag/abc/

    I would like to remove the tag prefix within my url.
    How can I do that?


    Thank you very much for any little help!

Viewing 1 replies (of 1 total)
  • Removing the tag prefix could possibly pose a conflict with a post/page but if you must, give the following code a try, insert into your functions.php file:

    function remove_tag_prefix( $rules ) {
        foreach ( $rules as $rule => $rewrite ) {
            if ( preg_match( '/tag\/(.+)/', $rule, $matches ) ) {
                $new_rule = str_replace( 'tag/', '', $rule );
                unset( $rules[ $rule ] );
                $rules[ $new_rule ] = $rewrite;
            }
        }
        return $rules;
    }
    add_filter( 'rewrite_rules_array', 'remove_tag_prefix' );
    

    This code uses the preg_match function to match the “tag/” portion of the rule, and then removes it from the rule using str_replace. The modified rules are returned, effectively removing the tag prefix from the URLs.

    After adding this code, you should flush the rewrite rules for the changes to take effect. You can do this by going to “Settings” -> “Permalinks” in your WordPress dashboard and simply clicking the “Save Changes” button.

Viewing 1 replies (of 1 total)
  • The topic ‘How To Remove Tag Prefix in URL?’ is closed to new replies.