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:
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:
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:
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:
If the og:image tag is set manually (using Yoast SEO or other popular plugins, for example), use that image.
Try to find the first image in the post.
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.
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:
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: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: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:The revised function might look something like this (untested):
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.