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.

Let me preface by saying I don't really understand Add_Filter, but I think that I want to be using it here. If not please let me know.

I want to modify wp_get_attachment_link so that the link url changes. So for example if I click on a thumbnail in a gallery instead of going directly to the file I want it to go to www.foo.com.

So what I would like to do is replace what wp_get_attachment_link does via add_filter. But I can't figure out how add_filter works. How do I get the parameters from the original function?

Original Function Call

wp_get_attachment_link($id, $size, $permalink, $icon, $text);

Filter

add_filter( 'wp_get_attachment_link', 'modify_attachment_link');

function modify_attachment_link() {
    //how do i access $id, $size, $permalink, $icon and $text???
    $foo = $id.$permalink;
    return $foo;
}
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Look at the function in wp-includes/post-template.php. There you see what information you can get:

apply_filters(
    'wp_get_attachment_link'
,   "<a href='$url' title='$post_title'>$link_text</a>"
,   $id
,   $size
,   $permalink
,   $icon
,   $text 
);

Note that you cannot access the $link_text and the $_post object as a standalone variables. Bug? Bug!

In your filter you cannot change the order of the arguments, just the number.

So add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 2 ); will give you the link markup and $id. The highest number of available arguments is 6.

The return value of your function will replace the first argument.

An (untested) example for changing the link URL:

/**
 * Replaces the URL for an attachment link.
 *
 * @param  string $markup     Original link markup
 * @param  int    $id         Post id
 * @param  mixed  $size       Image size, array or string
 * @param  string $permalink  URL
 * @param  bool   $icon       Use an icon?
 * @param  bool   $text       Use text?
 * @return string             New markup
 */
function modify_attachment_link( $markup, $id, $size, $permalink, $icon, $text )
{
    // We need just thumbnails.
    if ( 'thumbnail' !== $size )
    {   // Return the original string untouched.
        return $markup;
    }

    // We have stored the new URL in a post meta field.
    // See http://wordpress.stackexchange.com/q/3097 for an example.
    $new_url = get_post_meta( $id, 'extra_url', TRUE );

    if ( empty ( $new_url ) )
    {   // There is no URL.
        return $markup;
    }

    // Recreate the missing information.
    $_post      = & get_post( $id );
    $post_title = esc_attr( $_post->post_title );

    if ( $text ) 
    {
        $link_text = esc_attr( $text );
    } 
    elseif ( 
           ( is_int( $size )    && $size != 0 ) 
        or ( is_string( $size ) && $size != 'none' ) 
        or $size != FALSE 
    ) 
    {
        $link_text = wp_get_attachment_image( $id, $size, $icon );
    } 
    else 
    {
        $link_text = '';
    }

    if ( trim( $link_text ) == '' )
    {
        $link_text = $_post->post_title;
    }

    return "<a href='$new_url' title='$post_title'>$link_text</a>";
}

add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 6 );

Further reading:

share|improve this answer
    
@tascho - thank you for the example. This really helped me to understand how add_filter worked. I read the Wordpress documentation, but it just didn't make sense to me. Yours makes perfect sense. Also thank you for the further reading. My problem was not searching on the word "attachment." I am still getting used to the wordpress nomenclature. –  mrtsherman May 10 '11 at 14:33
    
Loving this answer. Thanks for laying it out all nice and like! +1 –  Foxinni Aug 28 '13 at 11:28
add comment
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 5 );

function modify_attachment_link($id, $size, $permalink, $icon, $text) {
    $foo = $id.$permalink;
    return $foo;
}

See the source.

The last two arguments are priority and number of args. If you don't specify the number of args of anything above one (IIRC) it will throw an error.

share|improve this answer
    
From toscho's example it looks like yours would not work. It appears that $markup (his variable) would be stuffed into $id (yours) instead. True? –  mrtsherman May 10 '11 at 18:49
    
Oh, you have a good point. Yes. The hook doesn't support the markup arg so you might have to try and get it in another way. –  dpgtfc May 20 '11 at 4:46
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.