WordPress add filter to wp_get_attachment_link

I need custom class for filter to wp_get_attachment_link. So what I so:

function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );

It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page I mean I don't need a custom class at this case. Any help please?

And core function for wp_get_attachment_link is:

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 = esc_attr( $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 );
}

So I mean if ( $permalink ) I don't need to add custom class for this function.

Upvotes: 0

Views: 3325

Answers (1)

AriePutranto
AriePutranto

Reputation: 401

Try

function modify_attachment_link( $markup, $id, $size, $permalink ) {
    global $post;
    if ( ! $permalink ) {
        $markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
    }
    return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );

That may work

Upvotes: 2

Related Questions