wordpress add link to featured image WordPress add link to Featured Image add external link to wordpress featured image

WordPress add link to Featured Image

Glenn Blog Leave a Comment

This is snippet that we’ve used for ages on our site so I thought I’d share it with you. What it enables you to do is add an external link to the featured image on the main post page. This is particularly useful for linking to client websites, affiliate links or any manner of URLs where linking out to an alternate URL is desirable.

WordPress add External link to Featured Image

Just 2 steps

  1. Add the following WordPress snippet to your WordPress Theme functions.php file
    // Add External Link to Featured Image with Custom Field
    
    add_filter('post_thumbnail_html','add_external_link_on_page_post_thumbnail',10);
        function add_external_link_on_page_post_thumbnail( $html ) {
        if( is_singular() ) {
                global $post;
                $name = get_post_meta($post->ID, 'ExternalUrl', true);
                if( $name ) {
                        $html = '<a href="' . ( $name ) . '" target="_blank" >' . $html . '</a>';
                }
        }
        return $html;
        }
    
    

    Save your file

  2. Now compose your post as usual and add the featured image. Then drop down the Custom Fields panel on your post page and ‘Add New Custom Field’ ExternalUrl and then add your desired destination external URL in the ‘Value’ entry like the below and that’s it – your featured image will now link externally to the Link set.
    wordpress add link to featured image WordPress add link to Featured Image new custom field

You can test this feature by clicking the Featured Image on this page and this will take you back to the Slick Media home page in a new window 🙂

About the Author
wordpress add link to featured image WordPress add link to Featured Image 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced Web Designer / Web Developer (Webflow, WordPress, Weebly & Custom), Front-end / Back-end Developer & New Media Specialist, with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries, built up over a number of years.