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.

I'm working on this function of WordPress.

function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
    $id = intval( $id );
    $_post = get_post( $id );

    if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
        return __( 'Missing Attachment' );

    if ( $permalink )
        $url = get_attachment_link( $_post->ID );

    $post_title = esc_attr( $_post->post_title );

    if ( $text )
        $link_text = $text;
    elseif ( $size && 'none' != $size )
        $link_text = wp_get_attachment_image( $id, $size, $icon );
    else
        $link_text = '';

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

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

And I want to modify this line:

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

I want that link can output like this:

<a href='$url' title='$post_title' id='**my_wish_attachment_ID**'>$link_text</a>

Because, default it print out like this:

<a href='http://link-to-image' title='post-title-example'><img src="http://link-to-thumbnail.png" class="attachment-thumbnail" alt="post-title-example" /></a>

I want it print out like this:

<a href='http://link-to-image' title='post-title-example' id='post-title-example'><img src="http://link-to-thumbnail.png" class="attachment-thumbnail" alt="post-title-example" /></a>

My wished attachment ID could be the same with 'post-title-example'.

I have tried many ways and searched Google a lot. But it doesn't work.

Can you help me? Thank you.

share|improve this question
    
What ways have you tried? –  s_ha_dum Feb 17 '13 at 15:53
    
I tried this way: add_filter('wp_get_attachment_link', 'add_id_into_link'); function add_id_into_link($link) { global $post; return str_replace('<a href', '<a id="'. $attachment_ID .'" href', $link); } –  SangDth Feb 17 '13 at 16:14
    
it does not work with $post_ID or $post_title or $_post->post_title also :( –  SangDth Feb 17 '13 at 16:19

1 Answer 1

up vote 0 down vote accepted

You have to supply a argument count when adding the filter callback, and add the arguments you are expecting to receive to your callback function. Looking at the wp_get_attachment_link source you can tell that 6 arguments is supplied when applying the filters (the link markup and $id, $size, $permalink, $icon, $text). Here's how you could would do just that:

add_filter('wp_get_attachment_link', 'add_id_into_link', 10, 6);
function add_id_into_link($link, $id = null, $size = null, $permalink = null, $icon = null, $text = null) {
    return str_replace('<a href', '<a id="'. $id .'" href', $link);
} 
share|improve this answer
    
hello, thank you very much. But your code just print out the number of ID, I need it like post_title so I take a look and follow your advice, here is my code: add_filter('wp_get_attachment_link', 'add_id_into_link', 10, 6); function add_id_into_link($link, $id = null, $size = null, $permalink = null, $icon = null, $text = null) { $id = intval( $id ); $_post = get_post( $id ); $post_title = esc_attr( $_post->post_title ); return str_replace('<a href', '<a id="'. $post_title .'" href', $link); } And it works! Is it ok right now? –  SangDth Feb 17 '13 at 18:37
    
I don't know how to add code in beautiful way like you @Simon –  SangDth Feb 17 '13 at 18:37
    
I'm sorry, I didn't quite understand what data you wanted in the id-attribute, but you seem to have solved that just fine. Happy to help! Regarding code formatting I think it's not just possible to use indentation and syntax highlight in comments, only in questions/answers. –  Simon Feb 17 '13 at 19:17

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.