Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
thz_opengraph
<?php
function thz_opengraph() {
if (defined('WPSEO_VERSION')) {
// if Yoast
if (get_post_type() == 'docs') {
$og_img = 'CUSTOM IMAGE IF DOCS';
$og = '<meta property="og:image" content="' . $og_img . '"/>';
echo $og;
}
} else {
global $post;
if (get_post_type() == 'docs') {
$og_img = 'CUSTOM IMAGE IF DOCS';
} else {
// default og_img
$og_img = get_stylesheet_directory_uri() . '/admin-splash.jpg';
if ( has_post_thumbnail($post->ID) ) {
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
$og_img = isset($img_src[0]) ? $img_src[0] : $og_img;
}
}
if ( is_front_page() ) {
$excerpt = 'CUSTOM EXCERPT IF FRONTPAGE';
} else {
$excerpt = $post->post_excerpt;
if ($excerpt != '') {
$excerpt = strip_tags($post->post_excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
}
$type = is_front_page() ? 'website' : 'article';
$og = '<meta property="og:title" content="' . get_the_title() . '"/>';
$og .= '<meta property="og:description" content="' . $excerpt . '"/>';
$og .= '<meta property="og:type" content="' . $type . '"/>';
$og .= '<meta property="og:url" content="' . get_the_permalink() . '"/>';
$og .= '<meta property="og:site_name" content="YOUR_DOMAIN_NAME"/>';
$og .= '<meta property="og:image" content="' . $og_img . '"/>';
echo $og;
}
}
add_action('wp_head', 'thz_opengraph', 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment