Skip to content

Instantly share code, notes, and snippets.

@lillyfreitas
Last active May 19, 2018 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lillyfreitas/facb462d3a6ccf98f1a291880e3f0b49 to your computer and use it in GitHub Desktop.
Save lillyfreitas/facb462d3a6ccf98f1a291880e3f0b49 to your computer and use it in GitHub Desktop.
Adicionar manualmente tags Open Graph em tema do WordPress
<!-- Compartilhamento em redes sociais -->
<!-- OPEN GRAPH -->
<?php
$img_compartilhe = get_post_meta($post->ID,'_thumbnail_id',false);
$img_compartilhe = wp_get_attachment_image_src($img_compartilhe[0], false);
$img_compartilhe = $img_compartilhe[0];
$img_padrao = get_bloginfo('stylesheet_directory').'/SUAIMAGEM.jpg';
// Recupera a URL da categoria
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<meta content="pt_br" property="og:locale">
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@SEUPERFILNOTWITTER" />
<meta property="og:type" content="<?php if ( is_single() || is_page() ) { echo "article"; } else { echo "website"; } ?>">
<meta property="og:site_name" content="<?php bloginfo('name'); ?>">
<meta property="og:url" content="<?php if ( is_home() ) { echo bloginfo('url');}
elseif ( is_category() ) { echo $category_link; } else { echo the_permalink(); } ?>"/>
<meta name="twitter:title" property="og:title" content="<?php if ( is_home() ) { echo bloginfo('name'); }
elseif ( is_page( 2 )) { echo "NOME DO SEU SITE OU EMPRESA"; }
elseif ( is_category() ) { echo wp_title('&#124;', true, 'right'), bloginfo('name'); }
else { echo single_post_title(''); } ?> " />
<meta name="twitter:description" property="og:description" content="<?php
if ( is_home() ) { echo bloginfo('description'); }
elseif ( is_page( 2 )) { echo "DESCRIÇÃO DO SEU SITE OU EMPRESA"; }
else { while(have_posts()):the_post();
echo $excerpt = strip_tags( get_the_excerpt() );
//echo substr($excerpt,0, 300);
//if (strlen($excerpt) > 299){ echo '...';}
endwhile; }
?>"/>
<meta name="twitter:image" property="og:image" content="<?php if ( is_home() || is_category() ) { echo $img_padrao; }
else { if ( $img_compartilhe[0] == null ) { echo $img_padrao; } else { echo $img_compartilhe; } } ?>" />
<meta property="og:image:type" content="image/png">
@lillyfreitas
Copy link
Author

lillyfreitas commented Jan 1, 2018

Edite o código do header.php do tema FILHO(*) do WordPress:

  1. Adicionar na tag <html> os atributos xmlns:og e xmlns:fb:
    <html <?php language_attributes(); ?> xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">

  2. Copie o código acima e cole antes do fim da tag <head> do arquivo header.php do seu tema filho

  3. Altere os itens em maiúscula para os valores de seu site:

  • linha 7: SUAIMAGEM.jpg
  • linha 17: @SEUPERFILNOTWITTER
  • linha 27: NOME DO SEU SITE OU EMPRESA
  • linha 36: DESCRIÇÃO DO SEU SITE OU EMPRESA
  1. Altere o id da página inicial do seu site nas linhas 27 e 36
  2. Nas linhas 40 e 41 há 2 alternativas para pegar a descrição, limitando o número de caracteres.
  3. Teste em http://developers.facebook.com/tools/debug/ e https://cards-dev.twitter.com/validator
  4. Faça os ajustes necessários

(*) Não edite diretamente no tema original, se não for de sua autoria. Saiba mais sobre temas filho no WordPress.


Observação: este código não é originalmente de minha autoria, embora eu tenha feito ajustes, mas no momento não encontro a fonte :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment