Open graph (wordpress)

Add open graph metadata.

<?php
/**
 * Add open graph metadata.
 */
add_action('wp_head', function() {
  if (is_search() || is_404() || post_password_required()) {
    return;
  }
  $og_title = '';
  $og_type = 'website';
  $og_url = '';
  $og_image = '';
  if (file_exists(get_theme_file_path('/assets/img/generic.jpg'))) {
    $og_image = get_theme_file_uri('/assets/img/generic.jpg');
  }
  $og_description = 'Visit the page for more.';
  $og_site_name = get_bloginfo('name');
  $og_locale = get_bloginfo('language');
  if (is_singular()) {
    $og_title = get_the_title();
    $og_type = 'article';
    $og_url = get_permalink();
    if (has_post_thumbnail()) {
      $og_image = get_the_post_thumbnail_url();
    } else {
      preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content(), $matches);
      if (!empty($matches)) {
        $og_image = $matches[1];
      }
    }
    if (get_the_excerpt()) {
      $og_description = get_the_excerpt();
    } elseif (get_the_content()) {
      $og_description = get_the_content();
    }
  }
  if (is_attachment()) {
    $attachment = wp_prepare_attachment_for_js(get_the_ID());
    if ('image' === $attachment['type']) {
      $sizes = $attachment['sizes'];
      if (array_key_exists('large', $sizes)) {
        $size = 'large';
      } elseif (array_key_exists('medium', $sizes)) {
        $size = 'medium';
      } elseif (array_key_exists('thumbnail', $sizes)) {
        $size = 'thumbnail';
      } else {
        $size = 'full';
      }
      if ($size) {
        $og_image = $attachment['sizes'][$size]['url'];
      }
    }
    if ($attachment['caption']) {
      $og_description = $attachment['caption'];
    } elseif ($attachment['description']) {
      $og_description = $attachment['description'];
    } elseif ($attachment['alt']) {
      $og_description = $attachment['alt'];
    }
  }
  if (is_front_page()) {
    $og_title = get_bloginfo('name');
    $og_type = 'website';
    $og_url = home_url('/');
    if (!empty(get_bloginfo('description')) && 'Just another WordPress site' !== get_bloginfo('description')) {
      $og_description = get_bloginfo('description');
    }
  }
  if (is_home() && !is_front_page()) {
    $og_title = get_the_title(get_option('page_for_posts'));
    $og_url = get_permalink(get_option('page_for_posts'));
    if (!empty(get_bloginfo('description')) && 'Just another WordPress site' !== get_bloginfo('description')) {
      $og_description = get_bloginfo('description');
    }
  }
  if (is_archive()) {
    $og_title = get_the_archive_title();
    $og_url = get_category_link(get_queried_object_id());
    if (!empty(get_the_archive_description())) {
      $og_description = get_the_archive_description();
    }
    if (is_author()) {
      $og_url = get_author_posts_url(get_the_author_meta('ID'));
    }
    if (is_date()) {
      if (is_day()) {
        $og_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day'));
      } elseif (is_month()) {
        $og_url = get_month_link(get_query_var('year'), get_query_var('monthnum'));
      } elseif (is_year()) {
        $og_url = get_year_link(get_query_var('year'));
      }
    }
    if (is_post_type_archive()) {
      $og_url = get_post_type_archive_link(get_post_type());
    }
  }
  function sanitize($input = '') {
    $input = wp_trim_words(
      preg_replace('@https?://[\S]+@', '', str_replace('"', "'", strip_shortcodes(wp_strip_all_tags($input))))
    );
    return $input;
  }
  $og_title = sanitize($og_title);
  $og_description = sanitize($og_description);
  if (!empty($og_title)) echo "\n", '<meta property="og:title" content="', esc_html($og_title), '">', "\n";
  if (!empty($og_type)) echo '<meta property="og:type" content="', esc_html($og_type), '">', "\n";
  if (!empty($og_url)) echo '<meta property="og:url" content="', esc_url($og_url), '">', "\n";
  if (!empty($og_image)) echo '<meta property="og:image" content="', esc_url($og_image), '">', "\n";
  if (!empty($og_description)) echo '<meta property="og:description" content="', esc_html($og_description), '">', "\n";
  if (!empty($og_site_name)) echo '<meta property="og:site_name" content="', esc_html($og_site_name), '">', "\n";
  if (!empty($og_locale)) echo '<meta property="og:locale" content="', esc_html($og_locale), '">', "\n\n";
});
?>

functions.php

< toolbox