Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active April 23, 2018 12:17
Show Gist options
  • Select an option

  • Save morgyface/0dfeaa004a30e9f472fe965350cce4ed to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/0dfeaa004a30e9f472fe965350cce4ed to your computer and use it in GitHub Desktop.
WordPress | Open Graph Include
<?php
if ( is_archive() ) {
$post_type = get_query_var( 'post_type' );
$og_url = get_post_type_archive_link( $post_type );
$og_title = post_type_archive_title('', false);
$page_object = get_page_by_title( $og_title );
$post_id = $page_object->ID;
} else {
$og_url = get_permalink();
$og_title = get_the_title();
$post_id = get_the_ID();
}
$og_type = 'article';
?>
<meta property="og:url" content="<?php echo esc_url( $og_url ); ?>" />
<meta property="og:description" content="<?php echo meta_description(155); ?>" />
<meta property="og:title" content="<?php echo $og_title; ?>" />
<meta property="og:type" content="<?php echo $og_type; ?>" />
<?php
// Image needs to be at least 1200 x 630 pixels
if ( has_post_thumbnail($post_id) ) {
$image_id = get_post_thumbnail_id($post_id);
$image_src_large = wp_get_attachment_image_src($image_id, 'xl');
$image_src_small = wp_get_attachment_image_src($image_id, 'md');
if( $image_src_large[1] == '1200' ) {
$image_url = $image_src_large[0];
} else {
$image_url = $image_src_small[0];
}
?>
<meta property="og:image" content="<?php echo $image_url; ?>" />
<?php } ?>
<?php if ( get_locale() ) { ?>
<meta property="og:locale" content="<?php echo get_locale(); ?>" />
<?php } ?>
@morgyface
Copy link
Author

Open Graph

Needs this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment