osompress / genesis-simple-share

Plugin: Genesis Simple Share
36 stars 16 forks source link

Pinterest shows no image or title for custom post types #41

Open nickcernis opened 9 years ago

nickcernis commented 9 years ago

On custom post types such as Rainmaker landing pages (e.g. Monument sales template), clicking the Pinterest button results in a blank description and image in the Pinterest popup, even though the page has a title and an image:

pinterest___pin_it__and_monument_test_-_test_site

This appears to be because get_first_image() in front-end.php looks for a fallback image in the main WP content, which won't always be present in a custom post type page template:

    function get_first_image( ) { 

        $content = get_the_content();

        $output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $matches);  

        if( $output )
            return $matches[1][0]; 

        return false;

    }

If no default Pinterest image is set, genesis_get_option( 'image_url', 'genesis_simple_share' ); on line 350 will also return false, $image will be empty, and no media data will get passed to the Pinterest button:

$button = 'pinterest' == $icon && $image ?  " pinterest: { media: '$image', description: '$description' }" : $button;

This also affects regular posts and pages where no image is present, as well as widgetised page templates with no post content. (Attempting to share with Pinterest results in a blank image and no description.)

I suggest that we refactor get_first_image() to find a fallback image in the following order:

  1. If the og:image tag is set manually (using Yoast SEO or other popular plugins, for example), use that image.
  2. Try to find the first image in the post.
  3. Fallback to the default Pinterest image from the Simple Share settings

The revised function might look something like this (untested):

function get_fallback_image( ) {

    $image = false;

    // Look for a custom Opengraph image
    if ( class_exists( 'WPSEO_Meta' ) )
        $image = WPSEO_Meta::get_value( 'opengraph-image' );

    // Look for an image in the post content
    if ( !$image ) {
        $content = get_the_content();

        $output = preg_match_all( '/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $matches );

        if ( $output )
            $image = $matches[1][0];
    }

    // Fall back to the default Pinterest image
    if ( !$image )
        $image = genesis_get_option( 'image_url', 'genesis_simple_share' );

    return $image ;

}  

There are Rainmaker customers who would like to be able to use the Pinterest share button on landing pages, but I'm sure this could benefit regular WP sites that use widgetised templates or custom post types too.

katyfb13 commented 9 years ago

Looks like this is not going to make it into 2.2 so moving to 2.3.1