Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

** UPDATED BELOW **

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":

http://www.myweb.com/search?I_DSC=dish&I_DSC_AND=t&_ACT=search

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

needs to be replaced with:

http://www.myweb.com/search?I_DSC=

then the $link followed by

&I_DSC_AND=t&_ACT=search

the line:

http://www.myweb.com/search?I_DSC=$link&I_DSC_AND=t&_ACT=search

won't work. Is there some alternative?

Thanks again - it is really appreciated.

share|improve this question
add comment

2 Answers

There's a filter in place specifically for tag links, can be found in source here.
http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/category-template.php#L780

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
add comment

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
add comment

Your Answer

 
discard

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