Skip to content

Commit

Permalink
Post thumbnail alt enhancements
Browse files Browse the repository at this point in the history
Fixes #186

* When displaying the post thumbnail, the Media Handler will first use the image's alt tag set in the Media editor. If alt tag is empty, then it will use the post title as a fallback. Filter `tptn_thumb_use_image_alt` and set it to false to not use the alt tag. Filter `tptn_thumb_alt_fallback_post_title` and set it to false to disable the alt tag
  • Loading branch information
ajaydsouza committed Aug 15, 2023
1 parent 86296f4 commit 2cfa9e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
47 changes: 39 additions & 8 deletions includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ function crp_get_the_post_thumbnail( $args = array() ) {

$post_title = esc_attr( $result->post_title );

$output = '';
$postimage = '';
$pick = '';
$output = '';
$postimage = '';
$pick = '';
$attachment_id = '';

// Let's start fetching the thumbnail. First place to look is in the post meta defined in the Settings page.
if ( ! $postimage ) {
Expand Down Expand Up @@ -194,13 +195,43 @@ function crp_get_the_post_thumbnail( $args = array() ) {

$class = $args['class'] . ' crp_' . $pick;

if ( empty( $attachment_id ) && ! in_array( $pick, array( 'video_thumb', 'default_thumb', 'site_icon_max', 'site_icon_min' ), true ) ) {
$attachment_id = crp_get_attachment_id_from_url( $postimage );
}

/**
* Flag to use the image's alt text as the thumbnail alt text.
*
* @since 3.3.4
*
* @param bool $use_image_alt Flag to use the image's alt text as the thumbnail alt text.
*/
$use_image_alt = apply_filters( 'crp_thumb_use_image_alt', true );

/**
* Flag to use the post title as the thumbnail alt text if no alt text is found.
*
* @since 3.3.4
*
* @param bool $alt_fallback Flag to use the post title as the thumbnail alt text if no alt text is found.
*/
$alt_fallback = apply_filters( 'crp_thumb_alt_fallback_post_title', true );

if ( ! empty( $attachment_id ) && $use_image_alt ) {
$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
}

if ( empty( $alt ) ) {
$alt = $alt_fallback ? $post_title : '';
}

/**
* Filters the thumbnail classes and allows a filter function to add any more classes if needed.
*
* @since 2.2.2
*
* @param string $thumb_html Thumbnail HTML
* @param array $args Argument array
* @param array $args Arguments array
*/
$attr['class'] = apply_filters( 'crp_thumb_class', $class, $args );

Expand All @@ -209,18 +240,18 @@ function crp_get_the_post_thumbnail( $args = array() ) {
*
* @since 2.5.0
*
* @param string $post_title Thumbnail alt attribute
* @param array $args Argument array
* @param string $alt Thumbnail alt attribute
* @param array $args Arguments array
*/
$attr['alt'] = apply_filters( 'crp_thumb_alt', $post_title, $args );
$attr['alt'] = apply_filters( 'crp_thumb_alt', $alt, $args );

/**
* Filters the thumbnail title.
*
* @since 2.6.0
*
* @param string $post_title Thumbnail title attribute
* @param array $args Argument array
* @param array $args Arguments array
*/
$attr['title'] = apply_filters( 'crp_thumb_title', $post_title, $args );

Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ You can insert the related posts anywhere in your post using the `[crp]` shortco

Release post: [https://webberzone.com/blog/contextual-related-posts-v3-3-0/](https://webberzone.com/blog/contextual-related-posts-v3-3-0/)

* Enhancements/Modifications:
* When displaying the post thumbnail, the Media Handler will first use the image's alt tag set in the Media editor. If alt tag is empty, then it will use the post title as a fallback. Filter `tptn_thumb_use_image_alt` and set it to false to not use the alt tag. Filter `tptn_thumb_alt_fallback_post_title` and set it to false to disable the alt tag

* Bug Fixes:
* Fix duplicate display of related posts when using reusable blocks or a plugin that inserts pages

Expand Down

0 comments on commit 2cfa9e3

Please sign in to comment.