<?php

// Add Facebook Open Graph and Twitter Card to Head
add_action('wp_head', 'open_graph_twitter_card', 2);

function open_graph_twitter_card()
{
  // Get the post or page ID
  $post_id = get_the_ID();

  // Get the post or page title
  $title = get_the_title($post_id);

  // Get the post or page excerpt (description)
  $description = (get_the_excerpt()) ? get_the_excerpt() : get_bloginfo('description');

  // Get the post or page URL
  $url = get_permalink($post_id);

  // Set default image url
  $default_image_url = get_theme_file_uri('/screenshot.png');

  // Get the post thumbnail (featured image) URL
  $thumbnail = get_the_post_thumbnail_url($post_id);
?>
  <!-- Open Graph meta tags -->
  <meta property="og:title" content="<?php echo $title . ' - ' . get_bloginfo('name');  ?>" />
  <meta property="og:description" content="<?php echo $description ?>" />
  <meta property="og:url" content="<?php echo $url ?>" />
  <meta property="og:image" content="<?php echo $default_image_url ?>" />
  <!-- Twitter Card meta tags -->
  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="<?php echo $title . ' - ' . get_bloginfo('name');  ?>" />
  <meta name="twitter:description" content="<?php echo $description ?>" />
  <meta name="twitter:image" content="<?php echo $default_image_url ?>" />
<?php

}
