• Skip to main content
  • Skip to primary sidebar
  • Business
  • WordPress
  • Security
  • Marketing
  • Publicity
  • Computing
  • Resources
  • Whatevs

Purely Wordpress

Just The Tech, News, and Info We Deem To Post

  • Home
  • About
  • Contact
  • Contribute
You are here: Home / Code Snippets / Add Open Graph To Genesis

Add Open Graph To Genesis

Genesis Theme Framework Code Snippets

Add the following code to your WordPress theme’s function.php file, or a functionality plugin to add Open Graph To Genesis.

/** ADD OPEN GRAPH TO GENESIS THEME
//* 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();
  }

?>



<?php
}

by Jack Alltrade on January 5, 2018

Filed Under: Code Snippets Tagged With: Code, Genesis Framework, Open Graph Protocol, WordPress Themes

Reader Interactions

Share Your Two Cents Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • Code Snippets
  • Development
  • File Management
  • Genesis Framework
  • Hosting
  • Legacy
  • Plugins
  • SEO
  • Tech Terms
  • Themes
  • User Experience
  • User Interface
  • WordPress Core
  • WordPress Functionality

Warning! Use at your own risk!

As always, use at your own risk and remember to backup your site prior to inserting new code.

© Copyright Jack Alltrade & Associates 2025 · Purely Supplemental™ is a trademark of Jack Alltrade & Associates