Replace a part of url generated by get_term_link

Is there a way to edit a text in url given by get_term_link()

I want to replace this text listing-category/ in all the links generated by get_term_link()

Till now I have been using the following code to do it manually wherever i wanted but now I realize that it will be efficient .

$term_link= str_replace('listing-category/', 'jobs/?fwp_cate=", get_term_link( $term ));
$term_link= rtrim($term_link, "https://wordpress.stackexchange.com/");

So is there a way to apply the above code in all the get_term_link()

Please help me out on this

1 Answer
1

Yes, there is a hook “term_link” which you can use for this purpose.
Here you can find documentation.
https://codex.wordpress.org/Plugin_API/Filter_Reference/term_link

so the solution will be like this

add_filter('term_link', 'term_link_filter', 10, 3);
function term_link_filter( $url, $term, $taxonomy ) {

  $url= str_replace('listing-category/', 'jobs/?fwp_cate=", $url);
  $url= rtrim($url, "https://wordpress.stackexchange.com/");

    return $url . "#results";

}

Leave a Comment