Created
September 26, 2013 13:53
-
-
Save rsharrer/6714572 to your computer and use it in GitHub Desktop.
Facebook open graph snippet to set default image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function diww_facebook_image() { | |
| echo '<meta property="fb:admins" content="ADMIN_ID" />'; | |
| echo '<meta property="og:title" content="' . get_the_title() . '" />'; | |
| echo '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />'; | |
| global $post; | |
| if ( is_singular() ) { // only if a single post or page | |
| echo '<meta property="og:type" content="article" />'; | |
| echo '<meta property="og:url" content="' . get_permalink() . '" />'; | |
| if (has_post_thumbnail( $post->ID )) { // use featured image if there is one | |
| $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); | |
| echo '<meta property="og:image" content="' . esc_attr( $feat_image[0] ) . '" />'; | |
| }else{ // use site logo in case no featured image | |
| echo '<meta property="og:image" content="http://yourdomain.com/logo.png" />'; | |
| } | |
| } | |
| if ( is_home() ) { // for homepage only | |
| echo '<meta property="og:type" content="website" />'; | |
| echo '<meta property="og:url" content="' . get_bloginfo('url') . '" />'; | |
| echo '<meta property="og:image" content="http://yourdomain.com/logo.png" />'; | |
| } | |
| } | |
| add_action( 'wp_head', 'diww_facebook_image' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment