Skip to content

Instantly share code, notes, and snippets.

@StuffieStephie
Created November 1, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StuffieStephie/e8eae7727f78f6d4f3308539413aa71e to your computer and use it in GitHub Desktop.
Save StuffieStephie/e8eae7727f78f6d4f3308539413aa71e to your computer and use it in GitHub Desktop.
WP OG Tags
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php
$meta = array(
"title" => get_bloginfo( 'title' ),
"desc" => get_bloginfo( 'description'),
"url" => home_url( $_SERVER['REQUEST_URI'] ),
"keywords" => 'generic tags, for example',
"image" => get_stylesheet_directory_uri() . "/images/ba_fallback.png",
);
if ( is_single() ) {
$meta["title"] .= ' | '. get_the_title($post);
if(!empty(get_the_excerpt($post))){
$meta["desc"] = get_the_excerpt($post);
}
if(has_post_thumbnail($post->ID)){
$meta["image"] = get_the_post_thumbnail_url($post->ID, 'full');
}
$post_tags = get_the_tags($post->ID);
if ( $post_tags ) {
foreach( $post_tags as $tag ) {
$meta["keywords"] .= ', ' . $tag->name;
}
}
}
?>
<meta name="description" content="<?php echo $meta["desc"]; ?>" />
<meta name="keywords" content="<?php echo $meta["keywords"]; ?>" />
<!-- Open Graph -->
<meta property="og:locale" content="en_US" />
<meta property="og:title" content="<?php echo $meta["title"]; ?>" />
<meta property="og:description" content="<?php echo $meta["desc"]; ?>" />
<meta property="og:image" content="<?php echo $meta["image"]; ?>" />
<meta property="og:url" content="<?php echo $meta['url']; ?>" />
<meta property="og:site_name" content="<?php bloginfo('title'); ?>" />
<meta property="og:type" content="website" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment