WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to insert media with link to file (size=large) (2 posts)

  1. photocurio
    Member
    Posted 1 year ago #

    If I use the Insert Media button to add an image to a post, I have four options for a link:
    a. custom URL
    a. attachment page
    b. media file (this is always the full size file, which might be 3000 pixels)
    c. none

    I'd like it to link to a smaller media file, perhaps Large, or a custom image size. That would work well with my Lightbox/Colorbox plugin.

    (I know I could find the URL of the custom size and paste it into the custom URL box, but I don't want to tell a client to do that. )

  2. photocurio
    Member
    Posted 11 months ago #

    I did eventually find the solution to this problem. See Oikos Get Attachment Link Filter.
    Its brilliant:

    function oikos_get_attachment_link_filter( $content, $post_id, $size, $permalink ) {
        // Only do this if we're getting the file URL
        if (! $permalink) {
            // This returns an array of (url, width, height)
            $image = wp_get_attachment_image_src( $post_id, 'large' );
            $new_content = preg_replace('/href=\'(.*?)\'/', 'href=\'' . $image[0] . '\'', $content );
            return $new_content;
        } else {
            return $content;
        }
    }
    
    add_filter('wp_get_attachment_link', 'oikos_get_attachment_link_filter', 10, 4);

Topic Closed

This topic has been closed to new replies.

About this Topic