Skip to content

Instantly share code, notes, and snippets.

@AndreFCAmorim
Created November 6, 2022 15:28
Show Gist options
  • Save AndreFCAmorim/01b66845978a886f7e528a7457af4eda to your computer and use it in GitHub Desktop.
Save AndreFCAmorim/01b66845978a886f7e528a7457af4eda to your computer and use it in GitHub Desktop.
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 to wrap in an anchor.
* @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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment