WP Facebook Open Graph protocol
One fix for Default Image always overwriting Content Images (1 post)

  1. thedoctorandthedad
    Posted 2 years ago #

    Similarly to some earlier posts that are now closed to new comments, I found that if I included a Default Image URL in Settings, Facebook would always choose it when sharing a link, even if my post had other images in the content (which I would rather be the initially selected thumbnail).

    I'm not a pro coder or anything, but I did a little copying and pasting in wp-facebook-ogp.php, and I got it to work for me. I basically replaced lines 149-178 with this:

    // Find/output any images for use in the OGP tags
    		$wpfbogp_images = array();
    		// Only find images if it isn't the homepage and the fallback isn't being forced
    		if ( ! is_home() && $options['wpfbogp_force_fallback'] != 1 ) {
    			// Find featured thumbnail of the current post/page
    			if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post->ID ) ) {
    				$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    				$wpfbogp_images[] = $thumbnail_src[0]; // Add to images array
    			if ( wpfbogp_find_images() !== false && is_singular() ) { // Use our function to find post/page images
    				$wpfbogp_images = array_merge( $wpfbogp_images, wpfbogp_find_images() ); // Returns an array already, so merge into existing
    		// Here's the part I added, which was a combination of the "Make sure there were images..." section below, and the "Add the fallback image..." section which I deleted from above
    		if ( ! empty( $wpfbogp_images ) && is_array( $wpfbogp_images ) ) {
    		} else {
    		if ( isset( $options['wpfbogp_fallback_img'] ) && $options['wpfbogp_fallback_img'] != '') {
    			$wpfbogp_images[] = $options['wpfbogp_fallback_img']; // Add to images array
    		// Make sure there were images passed as an array and loop through/output each
    		if ( ! empty( $wpfbogp_images ) && is_array( $wpfbogp_images ) ) {
    			foreach ( $wpfbogp_images as $image ) {
    				echo '<meta property="og:image" content="' . esc_url( apply_filters( 'wpfbogp_image', $image ) ) . '"/>' . "\n";
    		} else {
    			// No images were outputted because they have no default image (at the very least)
    			echo "<!-- There is not an image here as you haven't set a default image in the plugin settings! -->\n";

    Now the plugin will find images in my content and add them to the array, and then ONLY add the Default Image to the array if no content images were found.

    I see that Chuck is working on a new version of this plugin right now, but hopefully this helps some of you in the meantime!


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Facebook Open Graph protocol
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by thedoctorandthedad
  • This topic is not resolved
  • WordPress version: 3.7.1


No tags yet.