Use Grow data for Pinterest images and descriptions

If you were previously using Grow (Social Pug) for defining custom Pinterest images and descriptions, you can use the following code to use that data for the Pinterest share button in Shared Counts.

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

/**
 * Pinterest Image from Grow
 *
 */
function be_sc_use_grow_pinterest_image( $image_url, $post_id, $link ) {

	// First check if Shared Counts - Pinterest Image specified
	$key = apply_filters( 'shared_counts_pinterest_image_meta_key', 'shared_counts_pinterest_image', 'image' );
	$image = get_post_meta( $post_id, $key, true );
	if( !empty( $image ) )
		return $image_url;

	$grow_share_options = get_post_meta( $post_id, 'dpsp_share_options' );
	if( is_array( $grow_share_options ) ) {
		if( isset( $grow_share_options[0]['custom_image_pinterest']['src'] ) && ! empty( $grow_share_options[0]['custom_image_pinterest']['src'] ) ) {
			$image_url = $grow_share_options[0]['custom_image_pinterest']['src'];
		}
	}
	return $image_url;
}
add_filter( 'shared_counts_single_image', 'be_sc_use_grow_pinterest_image', 10, 3 );

/**
 * Pinterest Description from Grow
 *
 */
function be_sc_use_grow_pinterest_desc( $link, $post_id, $style ) {

	// First check if Shared Counts - Pinterest Image specified
	$key = apply_filters( 'shared_counts_pinterest_image_meta_key', 'shared_counts_pinterest_image_description', 'desc' );
	$desc = get_post_meta( $post_id, $key, true );
	if( !empty( $desc ) )
		return $link;

	$description = false;
	$grow_share_options = get_post_meta( $post_id, 'dpsp_share_options' );
	if( is_array( $grow_share_options ) ) {
		if( isset( $grow_share_options[0]['custom_title_pinterest'] ) && ! empty( $grow_share_options[0]['custom_title_pinterest'] ) ) {
			$description = $grow_share_options[0]['custom_title_pinterest'];
		}
	}

	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_sc_use_grow_pinterest_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.