Skip to content

Instantly share code, notes, and snippets.

@vicainelli
Last active January 1, 2016 17:49
Show Gist options
  • Save vicainelli/8179624 to your computer and use it in GitHub Desktop.
Save vicainelli/8179624 to your computer and use it in GitHub Desktop.
WordPress Facebook Open Graph

Wordpress + Facebook Open Graph

Facebook Open Graph settings for a share posts!

<?php
// Facebook Open Graph settings
// og:type
function og_type() {
if (is_home() || is_page() || is_search()){
echo "website";
}else{
echo "article";
}
}
// og:url
function og_url() {
if (is_home()) {
echo esc_url( home_url( '/' ) );
}else{
echo get_permalink($post->ID);
}
}
// og:image
function og_image() {
if (is_home() || is_page()) {
bloginfo('template_url');echo '/images/facebook-share.jpg';
}else{
if (has_post_thumbnail()) {
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo $url;
}else{
bloginfo('template_url');echo '/images/facebook-share.jpg';
}
}
}
?>
<!DOCTYPE HTML>
<html lang="pt-BR">
<head>
<!-- Open Graph -->
<meta property="og:title" content="<?php wp_title('|',true,'right'); ?><?php bloginfo('name'); ?>" />
<meta property="og:type" content="<?php og_type(); ?>"/>
<meta property="og:url" content="<?php og_url(); ?>"/>
<meta property="og:image" content="<?php og_image(); ?>" />
<meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment