Created
March 12, 2019 12:10
-
-
Save danyj/fee862f3a3abe9efb81a86c7234558c7 to your computer and use it in GitHub Desktop.
thz_opengraph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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