Skip to content

Instantly share code, notes, and snippets.

@eriku
Created April 17, 2015 17:09
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 eriku/0d61926690c66ea32c41 to your computer and use it in GitHub Desktop.
Save eriku/0d61926690c66ea32c41 to your computer and use it in GitHub Desktop.
Open Graph & Twitter Cards
<?php
/**
* Open Graph & Twitter Card Meta Tags
*/
// Description
$description = false;
if ( is_single() || (is_page() && !is_front_page()) ) {
$description = $post->post_content;
$description = preg_replace('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $description);
$description = strip_tags(trim(substr($description, 0, 1500)));
} else {
$description = get_bloginfo('description');
}
// Image
$image = false;
if ( is_front_page() || ( get_field('hero_images') && get_the_ID() ) ) {
$image = get_field('hero_images')[0]['hero_image']['url'];
} elseif ( has_post_thumbnail() && !is_archive() ) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
$image = $image[0];
} else {
$image = get_bloginfo('template_url') . '/images/meta-logo.jpg';
}
// Title
$title = false;
if ( is_home() ) {
$title = get_bloginfo('name');
} else {
$title = get_the_title();
}
// URL
$url = false;
if ( is_home() ) {
$url = get_bloginfo('url');
} else {
$url = get_permalink();
}
?>
<meta name="description" content="<?php echo $description; ?>">
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
<meta property="og:url" content="<?php echo $url; ?>" />
<meta property="og:description" content="<?php echo $description; ?>" />
<meta property="og:image" content="<?php echo $image; ?>" />
<meta property="og:locale" content="en_US" />
<meta name="twitter:url" content="<?php echo $url; ?>" />
<meta name="twitter:title" content="<?php echo $title; ?>" />
<meta name="twitter:description" content="<?php echo $description; ?>" />
<meta name="twitter:image" content="<?php echo $image; ?>" />
<!--meta name="twitter:site" content="@" /-->
<meta property="fb:app_id" content="455144901301315" />
<meta property="fb:admins" content="727005715" />
<?php if ( is_home() ) : ?>
<!-- Business Open Graph Tags -->
<meta name="twitter:card" content="summary" />
<meta property="og:type" content="business.business" />
<meta property="place:location:latitude" content="32.811667" />
<meta property="place:location:longitude" content="-96.950833" />
<meta property="business:contact_data:street_address" content="PO Box 154843" />
<meta property="business:contact_data:locality" content="Irving" />
<meta property="business:contact_data:region" content="Texas" />
<meta property="business:contact_data:postal_code" content="75015-4843" />
<meta property="business:contact_data:country_name" content="United States of America" />
<meta property="business:contact_data:email" content="ftgmotorsports@gmail.com" />
<meta property="business:contact_data:phone_number" content="(972) 891-8293" />
<meta property="business:contact_data:website" content="<?php the_bloginfo('url'); ?>" />
<?php elseif ( get_post_type() == 'product' ) : ?>
<?php
$price = get_post_meta( get_the_ID(), '_price' )[0];
$availability = get_post_meta( get_the_ID(), '_stock_status' )[0];
?>
<!-- Product Open Graph Tags -->
<meta property="og:type" content="product" />
<meta property="product:price:amount" content="<?php echo $price; ?>" />
<meta property="product:price:currency" content="<?php echo get_woocommerce_currency(); ?>" />
<meta property="product:availability" content="<?php echo $availability; ?>" />
<meta name="twitter:card" content="product" />
<meta name="twitter:label1" content="Price">
<meta name="twitter:data1" content="<?php echo $price; ?> <?php echo get_woocommerce_currency(); ?>">
<meta name="twitter:label2" content="Availability">
<meta name="twitter:data2" content="<?php echo $availability; ?>">
<?php else : ?>
<meta name="twitter:card" content="summary" />
<meta property="og:type" content="website" />
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment