• Resolved Shmoo

    (@macpresss)


    Looking inside the post-template.php file I see this.

    ........
    /**
    	 * Filter a retrieved attachment page link.
    	 *
    	 * @since 2.7.0
    	 *
    	 * @param string      $link_html The page link HTML output.
    	 * @param int         $id        Post ID.
    	 * @param string      $size      Image size. Default 'thumbnail'.
    	 * @param bool        $permalink Whether to add permalink to image. Default false.
    	 * @param bool        $icon      Whether to include an icon. Default false.
    	 * @param string|bool $text      If string, will be link text. Default false.
    	 */
    	return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );

    And I would really like to add my own class to all attachment generated links by WordPress.
    So I tried to use the filter like this.

    function my_attachment_link_class( $link_html ) {
    
         $link_html = str_replace( '<a', '<a class="test"', $link_html );
    
        return $link_html;
    
    }
    add_filter( 'wp_get_attachment_link', 'my_attachment_link_class' );

    But for some reason I can’t get the default url markup, replace it and return it. ?

Viewing 1 replies (of 1 total)
  • Thread Starter Shmoo

    (@macpresss)

    This seems to work better.. It only applies to new added images via the media uploader. It’s not really a content filter. This adds a class to the link as soon as you use the media uploader/editor.

    function my_custom_imglink_class( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
    
    	$classes = 'image-popup';
    
    	if ( preg_match( '/<a.*? class=".*?">/', $html ) ) {
    
    		$html = preg_replace( '/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html );
    
    	}
    	else {
    
    		$html = preg_replace( '/(<a.*?)>/', '$1 class="' . $classes . '" >', $html );
    
    	}
    	return $html;
    
    }
    add_filter( 'image_send_to_editor', 'my_custom_imglink_class', 10, 8 );
Viewing 1 replies (of 1 total)

The topic ‘wp_get_attachment_link filter help’ is closed to new replies.