Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding Open Graph Meta Tags to the Genesis Theme Head
<?php
//* Call the First Image in a Post (Used in the Open Graph Call Below)
function catch_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Define default image
$first_img = "/images/defalut.jpg";
}
return $first_img;
}
//* Add Open Graph meta tag to Head
add_action( 'genesis_meta', 'fb_opengraph' );
function fb_opengraph() {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )[0];
}
else {
$img_src = catch_first_image();
}
?>
<meta property="og:image" content="<?php echo $img_src; ?>">
<meta property="og:title" content="<?php echo the_title(); ?>">
<meta property="og:url" content="<?php echo the_permalink(); ?>">
<meta property="og:description" content="<?php echo genesis_get_custom_field( '_genesis_description' ) ?>">
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment