André Amorim

Crafting Web Experiences

//

Automatically Link Featured Images to Posts on WordPress

<?php
/**
 * Wrap the thumbnail in a link to the post.
 * Only use this if your theme doesn't already wrap thumbnails in a link.
 *
 * @param string $html          The thumbnail HTML.
 * @param int    $post_id       The post ID.
 * @param int    $post_image_id The image ID.
 *
 * @return string
 */
function wpcode_snippet_autolink_featured_images($html, $post_id, $post_image_id) {
    $html = '<a href="' . get_permalink($post_id) . '" title="' . esc_attr(get_the_title($post_id)) . '">' . $html . '</a>';

    return $html;
}

add_filter('post_thumbnail_html', 'wpcode_snippet_autolink_featured_images', 20, 3);

Published date:

Modified date: