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;
}
Look at the function in
wp-includes/post-template.php
. There you see what information you can get: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:
Further reading:
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.