Use featured image description in Pinterest

When someone clicks your Pinterest sharing button, Shared Counts uses the current post’s featured image as the image, and the current post’s title as the description.

Want a more descriptive description? The code below will use the featured image’s description if provided, and if empty it will fall back to the post title.

This code snippet goes in a core functionality plugin or Code Snippets.

/**
 * Pinterest button, use image description 
 * @link https://sharedcountsplugin.com/2019/09/04/use-featured-image-description-in-pinterest/
 *
 * @param array $link
 * @param int $post_id
 * @param string $style 
 * @return array $link 
 */
function be_pinterest_image_description( $link, $post_id, $style ) {
	if( 'pinterest' === $link['type'] && has_post_thumbnail( $post_id ) ) {
		$description = get_post( get_post_thumbnail_id( $post_id ) )->post_content;
		if( empty( $description ) )
			$description = $link['title'];
		$link['link'] = 'https://pinterest.com/pin/create/button/?url=' . $link['url'] . '&media=' . $link['img'] . '&description=' . rawurlencode( wp_strip_all_tags( $description ) );
	}
	return $link;
}
add_filter( 'shared_counts_link', 'be_pinterest_image_description', 9, 3 );Code language: PHP (php)

Use the image’s alt text

You may be using the alt text to describe the image instead of the description field. the code below will use the alt text as the description for Pinterest.

This code snippet goes in a core functionality plugin or Code Snippets.

/**
 * Pinterest button, use image alt text as description
 * @link https://sharedcountsplugin.com/2019/09/04/use-featured-image-description-in-pinterest#alt-text
 *
 * @param array $link
 * @param int $post_id
 * @param string $style
 * @return array $link
 */
function be_pinterest_image_description( $link, $post_id, $style ) {
	if( 'pinterest' === $link['type'] && has_post_thumbnail( $post_id ) ) {

		$description = get_post_meta( get_post_thumbnail_id( $post_id ), '_wp_attachment_image_alt', true );
		if( empty( $description ) )
			$description = $link['title'];
		$link['link'] = 'https://pinterest.com/pin/create/button/?url=' . $link['url'] . '&media=' . $link['img'] . '&description=' . rawurlencode( wp_strip_all_tags( $description ) );
	}
	return $link;
}
add_filter( 'shared_counts_link', 'be_pinterest_image_description', 9, 3 );Code language: PHP (php)

Filters used:

Published by Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributor to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.