WordPress.org

Ready to get started?Download WordPress

Forums

Facebook OG issue workaround (4 posts)

  1. michaeljnedell
    Member
    Posted 1 year ago #

    Hi - I am using this code

    //Adding the Open Graph in the Language Attributes
    function add_opengraph_doctype( $output ) {
    		return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
    	}
    add_filter('language_attributes', 'add_opengraph_doctype');
    
    //Lets add Open Graph Meta Info
    
    function insert_fb_in_head() {
    	global $post;
    	if ( !is_singular()) //if it is not a post or a page
    		return;
            echo '<meta property="fb:admins" content="YOUR USER ID"/>';
            echo '<meta property="og:title" content="' . get_the_title() . '"/>';
            echo '<meta property="og:type" content="article"/>';
            echo '<meta property="og:url" content="' . get_permalink() . '"/>';
            echo '<meta property="og:site_name" content="Your Site NAME Goes HERE"/>';
    	if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
    		$default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
    		echo '<meta property="og:image" content="' . $default_image . '"/>';
    	}
    	else{
    		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    		echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    	}
    	echo "
    ";
    }
    add_action( 'wp_head', 'insert_fb_in_head', 5 );

    To get Facebook to recognize the content on my site. There is a problem with https though, and FB does not recognize the images (this has been discussed on many FB forums.

    The workaround is to put the images into a subdomain of a separate domain - ok - so my default image shows now - good!

    BUT - you can't add a featured image through a URL - so to get the image for that post to show I want to create a custom field - and the value would be the image path in the sub-domain.

    Here is my question - How would I modify the above code to pull the custom field data INSTEAD of the featured image data.

    Thank you for all your help! You rock!!

  2. Spencer Cameron
    Member
    Posted 1 year ago #

    Hi michaeljnedell,

    Try replacing:

    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
    		$default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
    		echo '<meta property="og:image" content="' . $default_image . '"/>';
    	}
    	else{
    		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    		echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    	}

    With:

    $custom_post_thumbnail = get_post_meta( $post->ID, 'custom_field_name_for_image', true );
    if( ! empty( $custom_post_thumbnail ) )
            echo '<meta property="og:image" content="' . esc_attr( $custom_post_thumbnail ) . '"/>';
    else
            // Apply defaults here if you like.

    Let me know if you have any troubles/questions. :)

  3. michaeljnedell
    Member
    Posted 1 year ago #

    YESSS!!!!! You rock!

  4. Spencer Cameron
    Member
    Posted 1 year ago #

    Awesome! Let me know if you have any more problems! :)

Topic Closed

This topic has been closed to new replies.

About this Topic