-
-
Save fgrweb/dea36778d2cc2acd628f166e0d795b60 to your computer and use it in GitHub Desktop.
Adding Open Graph Meta Tags to the Genesis Theme Head
This file contains 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
<?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