Tasty Pins integration for Shared Counts

Tasty Pins is a popular plugin for optimizing your content for Pinterest. It includes a feature to specify a Pinterest-specific image and description, just like the Shared Counts – Pinterest Image addon.

The following code will let Shared Counts use the image and description you specify in Tasty Pins:

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

/**
 * Pinterest image from Tasty Pins.
 *
 * @param string $image_url Image URL.
 * @param int    $id Post ID.
 * @param array  $link Shared Counts Link.
 */
function be_shared_counts_tasty_pin_image( $image_url, $id, $link ) {
	if ( 'pinterest' !== $link['type'] ) {
		return $image_url;
	}

	$pinterest_image = get_post_meta( $id, 'tp_pinterest_hidden_image', true );
	if ( ! empty( $pinterest_image ) ) {
		$image_url = wp_get_attachment_image_url( $pinterest_image, 'full' );
	}

	return $image_url;
}
add_filter( 'shared_counts_single_image', 'be_shared_counts_tasty_pin_image', 10, 3 );

/**
 * Pinterest description from Tasty Pins
 *
 * @param array  $link Shared Counts Link.
 * @param int    $id Post ID.
 * @param string $style Link Style.
 */
function be_shared_counts_tasty_pin_desc( $link, $id, $style ) {
	if ( 'pinterest' !== $link['type'] ) {
		return $link;
	}

	$description = get_post_meta( $id, 'tp_pinterest_default_text', true );
	if ( empty( $description ) ) {
		return $link;
	}

	$url          = explode( 'description=', $link['link'] );
	$link['link'] = $url[0] . 'description=' . rawurlencode( wp_strip_all_tags( $description ) );
	return $link;
}
add_filter( 'shared_counts_link', 'be_shared_counts_tasty_pin_desc', 10, 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.