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:
shared_counts_link
shared_counts_single_image