Skip to content

Instantly share code, notes, and snippets.

@zergiocosta
Last active January 28, 2018 10:16
Show Gist options
  • Save zergiocosta/204d537c7b984133915d8db4523f6764 to your computer and use it in GitHub Desktop.
Save zergiocosta/204d537c7b984133915d8db4523f6764 to your computer and use it in GitHub Desktop.
How to work even better with meta tags on WP (This should be inside <head> tag)
<?php
/**
* Sets up theme default paths
*/
if(!defined('WP_SITE_URL')) { define('WP_SITE_URL', get_bloginfo('url')); }
if(!defined('WP_THEME_URL')) { define('WP_THEME_URL', get_stylesheet_directory_uri()); }
// General Variables
$mtitle_default = get_bloginfo('name');
$title_default = get_bloginfo('name');
$home_default = WP_SITE_URL;
$keys_default = ''; /* default site tags */
$link_default = WP_SITE_URL;
$desc_default = ''; /* default site description */
$image_default = WP_THEME_URL . '/screenshot.png';
if (is_single() || is_page()) {
global $post;
$title_default = get_the_title($post->ID);
$link_default = get_permalink();
$desc_default = get_post($post->ID);
$desc_default = strip_tags( $desc_default->post_content );
$desc_default = substr( $desc_default, 0, 250 );
if (has_post_thumbnail()){
$image_ID = get_post_thumbnail_id($post->ID);
$image_default = wp_get_attachment_image_src($image_ID, 'large');
$image_default = $image_default[0];
} else {
$image_default = WP_THEME_URL . '{restante-da-url-da-imagem}.png';
}
}
global $post;
if (!is_404()) {
$posttags = wp_get_post_tags( $post->ID );
}
?>
<link type="text/plain" rel="author" href="<?php echo WP_THEME_URL ?>/humans.txt" />
<meta name="copyright" content="&copy; Copyright <?php echo date('Y'); ?> - <?php echo $title_default; ?>" />
<meta name="keywords" content="<?php echo $keys_default; ?>, <?php if($posttags){foreach($posttags as $tag){echo $tag->name . ', ';}}; ?>" />
<meta name="url" content="<?php WP_SITE_URL ?>">
<?php if (!is_single()) { ?>
<meta name="author" content="República Interativa">
<meta name="description" content="<?php echo $desc_default; ?>" />
<?php } ?>
<?php
if(is_single() || is_page() || is_category() || is_home()) {
echo '<meta name="robots" content="all,noodp" />';
echo "\n";
}
else if(is_archive()) {
echo '<meta name="robots" content="noarchive,noodp" />';
echo "\n";
}
else if(is_search() || is_404()) {
echo '<meta name="robots" content="noindex,noarchive" />';
echo "\n";
}
?>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<!-- Schema.org -->
<meta itemprop="name" content="<?php echo $title_default; ?>">
<meta itemprop="description" content="<?php echo $desc_default; ?>">
<meta itemprop="image" content="<?php echo $image_default; ?>">
<!-- Open Graph Meta Data -->
<meta name="p:domain_verify" content=""/>
<meta property="og:title" content="<?php echo $title_default; ?>"/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="<?php echo $link_default; ?>"/>
<meta property="og:image" content="<?php echo $image_default; ?>"/>
<meta property="og:description" content="<?php echo $desc_default; ?>">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@repubinterativa">
<meta name="twitter:title" content="<?php echo $title_default; ?>">
<meta name="twitter:description" content="<?php echo $desc_default; ?>">
<meta name="twitter:creator" content="@repubinterativa">
<meta name="twitter:image" content="<?php echo $image_default; ?>">
<!-- Google Geo Location Meta Data -->
<meta name="geo.region" content="BR-BA" />
<meta name="geo.placename" content="Salvador" />
<meta name="geo.position" content="{coordenadas}" />
<meta name="ICBM" content="{coordenadas}" />
<!-- Dublin Core Meta Data -->
<meta name="dc.language" content="PT-BR">
<meta name="dc.creator" content="<?php echo $mtitle_default; ?>">
<meta name="dc.publisher" content="<?php echo $mtitle_default; ?>">
<meta name="dc.source" content="<?php echo $home_default; ?>">
<meta name="dc.relation" content="<?php echo $link_default; ?>">
<meta name="dc.title" content="<?php echo $title_default; ?>">
<meta name="dc.keywords" content="<?php echo $keys_default; ?>, <?php if($posttags){foreach($posttags as $tag){echo $tag->name . ', ';}}; ?>">
<meta name="dc.subject" content="Desenvolvimento web e mobile (Web and mobile development)">
<meta name="dc.description" content="<?php echo $desc_default; ?>">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment