WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Original Question: Anyone know how to create a function that changes the url of the_tags so that when one is clicked on, the function searches another domain?

** UPDATE **

IT WORKS! Thanks a million. What a star. :-)

ONE MORE Q: I have discovered that the search the link is being sent to is complicated. The search term below is "dish":


So the $replace_with = 'http://www.myweb.com/';

needs to be replaced with:


then the $link followed by


the line:


won't work. Is there some alternative?

Thanks again - it is really appreciated.

share|improve this question

There's a filter in place specifically for tag links, can be found in source here.

It's the first function you see on the above link, and it's this line that provides a hookable filter.

return apply_filters( 'tag_link', $taglink, $tag_id );

Here's an example of hooking a function to that hook..

add_filter( 'tag_link', 'new_tag_link', 100 );

function new_tag_link( $taglink ) {

    // What to find in the link
    $find = home_url();

    // What to replace it with
    $replace_with = 'http://example.com';

    // Run string replacement
    $taglink = str_replace( $find, $replace_with, $taglink );

    // Return the modified tag link
    return $taglink;

I have not tested the code because as indicated above it's only an example, but in theory the above should do exactly what you asked.

Hope that helps.

UPDATE: Code provided above works just fine now, you can also use the below code as an alternative, both will effectively do exactly the same though.

add_filter( 'term_links-post_tag', 'replace_tag_domain', 100 );

function replace_tag_domain( $links ) {

    // What to find in the link
    $find = home_url();

    // What to replace it with
    $replace_with = 'http://example.com';

    foreach( $links as $k => $link )
    // Run string replacement
        $links[$k] = str_replace( $find, $replace_with, $link );

    // Return the modified tag link
    return $links;

NOTE: Code can go into the functions file of your theme, that's where i do my testing.

share|improve this answer
Thanks a million! – Radd Jan 4 '11 at 17:47
Updated answer, tested both samples - worked in both cases. – t31os Jan 5 '11 at 10:17

Tell me (I'm trying to understand how Wordpress/php works).

The function has been added in functions.php yet is is not yet working. Do I need to declare/ register this filter elsewhere for it to work or should it happen straight off once in functions?

Thanks - really appreciated.

share|improve this answer
If you have more to add to the discussion please update the original question. I'll go test the code and report back.. :) – t31os Jan 5 '11 at 9:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.