Skip to content

Instantly share code, notes, and snippets.

@aschweigert
Created August 22, 2013 21:04
Show Gist options
  • Save aschweigert/6312749 to your computer and use it in GitHub Desktop.
Save aschweigert/6312749 to your computer and use it in GitHub Desktop.
WP metadata stuff (from http://github.com/inn/largo)
if ( ! function_exists( 'largo_opengraph' ) ) {
function largo_opengraph() {
global $current_url, $post;
// set a default thumbnail, if a post has a featured image use that instead
if ( is_single() && has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
$thumbnailURL = $image[0];
} else if ( of_get_option( 'logo_thumbnail_sq' ) ) {
$thumbnailURL = of_get_option( 'logo_thumbnail_sq' );
}
// start the output, some attributes will be the same for all page types ?>
<meta name="twitter:card" content="summary">
<?php
if ( of_get_option( 'twitter_link' ) )
echo '<meta name="twitter:site" content="@' . twitter_url_to_username( of_get_option( 'twitter_link' ) ) . '">';
?>
<?php // output appropriate OG tags by page type
if ( is_single() ) {
if ( have_posts() ) {
the_post(); // we need to queue up the post to get the post specific info
if ( get_the_author_meta( 'twitter' ) )
echo '<meta name="twitter:creator" content="@' . twitter_url_to_username( get_the_author_meta( 'twitter' ) ) . '">';
?>
<meta property="og:title" content="<?php the_title(); ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="<?php the_permalink(); ?>"/>
<meta property="og:description" content="<?php echo strip_tags( get_the_excerpt() ); ?>" />
<?php
} // have_posts
rewind_posts();
} elseif ( is_home() ) { ?>
<meta property="og:title" content="<?php bloginfo( 'name' ); echo ' - '; bloginfo('description'); ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo home_url(); ?>"/>
<meta property="og:description" content="<?php bloginfo( 'description' ); ?>" />
<?php
} else {
?>
<meta property="og:title" content="<?php bloginfo( 'name' ); wp_title(); ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="<?php echo $current_url; ?>"/>
<?php
//let's try to get a better description when available
if ( is_category() && category_description() ) {
$description = category_description();
} elseif ( is_author() ) {
if ( have_posts() ) {
the_post(); // we need to queue up the post to get the post specific info
if ( get_the_author_meta( 'description' ) )
$description = get_the_author_meta( 'description' );
}
rewind_posts();
} else {
$description = get_bloginfo( 'description' );
}
if ( $description ) {
echo '<meta property="og:description" content="' . strip_tags( $description ) . '" />';
}
} // else
// a few more attributes that are common to all page types
echo '<meta property="og:site_name" content="' . get_bloginfo() . '" />';
// thumbnail url
if ( $thumbnailURL )
echo '<meta property="og:image" content="' . $thumbnailURL . '" />';
// google author/publisher markup
// see: https://support.google.com/webmasters/answer/1408986
if ( of_get_option( 'gplus_link' ) )
echo '<link href="' . esc_url( of_get_option( 'gplus_link' ) ) . '" rel="publisher" />';
}
}
add_action( 'wp_head', 'largo_opengraph' );
if ( ! function_exists ( 'largo_seo' ) ) {
function largo_seo() {
// noindex for date archives (and optionally on all archive pages)
// if the blog is set to private wordpress already adds noindex,nofollow
if ( get_option( 'blog_public') ) {
if ( is_date() || ( is_archive() && of_get_option( 'noindex_archives' ) ) ) {
echo '<meta name="robots" content="noindex,follow" />';
}
}
// single posts get a bunch of other google news specific meta tags
if ( is_single() ) {
if ( have_posts() ) : the_post();
$permalink = get_permalink();
// use categories and tags for the news_keywords meta tag
// up to 10 terms per Google guidelines:
// https://support.google.com/news/publisher/answer/68297
if ( largo_has_categories_or_tags() ) {
echo '<meta name="news_keywords" content="';
largo_categories_and_tags( 10, true, false, false, ', ' );
echo '">';
}
// set the original-source meta tag
// see: http://googlenewsblog.blogspot.com/2010/11/credit-where-credit-is-due.html
echo '<meta name="original-source" content="'. $permalink .'" />';
// check for the existence of a custom field 'permalink'
// if it doesn't exist we'll just use the current url as the syndication source
if ( get_post_meta( get_the_ID(), 'permalink', true ) ) {
echo '<meta name="syndication-source" content="' . get_post_meta( get_the_ID(), 'permalink', true ) . '" />';
} else {
echo '<meta name="syndication-source" content="' . $permalink . '" />';
}
// add the standout metatag if this post is flagged with any of the terms in the prominence taxonomy
// see: https://support.google.com/news/publisher/answer/191283
if ( has_term( get_terms( 'prominence', 'fields=name' ), 'prominence' ) ) {
echo '<meta name="standout" content="' . $permalink . '"/>';
}
endif;
}
rewind_posts();
}
}
add_action( 'wp_head', 'largo_seo' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment