Skip to content

Instantly share code, notes, and snippets.

@pixeline
Last active August 13, 2018 13:37
Show Gist options
  • Save pixeline/044bafcc2daed5a7c355 to your computer and use it in GitHub Desktop.
Save pixeline/044bafcc2daed5a7c355 to your computer and use it in GitHub Desktop.
Generates metatags for wordpress
<?php
add_filter( 'customer', 'return_customer_info', 10, 1 );
function return_customer_info( $arg = '' ) {
/*
usage: echo apply_filters('customer', 'facebook');
*/
$customer = array(
'name' => 'Customer Name',
'facebook'=> 'https://www.facebook.com/customer',
'instagram'=>'https://www.instagram.com/customer/',
'youtube' => 'https://www.youtube.com/channel/customer',
'pinterest' => 'https://fr.pinterest.com/customer',
'googleplus' => 'https://plus.google.com/+customer',
'default_share_image'=> get_stylesheet_directory_uri().'/assets/media/customer_social_image.jpg',
'facebook_admin_id' => 'CUSTOMER FACEBOOK PAGE/APPLICATION ID',
);
return $customer[$arg];
}
add_action( 'wp_head', 'the_metatags' );
function the_metatags($data = ''){
/*
the_metatags() takes care of printing all useful SEO / opengraph metadata.
see get_the_metatags() for finetuning.
*/
if( empty($data) || !is_array($data) ){
$data = get_the_metatags();
}
foreach ($data as $key => $value){
$attribute = ($key === 'description') ? 'name' : 'property';
echo "\n".'<meta '.$attribute.'="' . $key . '" content="' . $value . '">';
}
}
function get_the_metatags(){
/*
get_the_metatags() massages an array with all useful SEO / opengraph meta tags.
*/
global $post;
$metatags = array(
'description' => prepare_string_for_meta(get_bloginfo('description')),
'fb:admins' => apply_filters('customer', 'facebook_admin_id'),
'og:type' => 'website',
'og:site_name' => get_bloginfo( 'name' ),
'article:published_time' => null, // ISO 8601
'article:modified_time' => null, // ISO 8601
'article:section' => null,
'article:tag' => null,
'og:title' => prepare_string_for_meta(get_the_title()),
'og:url' => get_bloginfo('url'),
'og:description' => prepare_string_for_meta(get_bloginfo('description')),
'og:image'=> apply_filters('customer', 'default_share_image'),
'twitter:card' => 'summary',
'twitter:site' => get_bloginfo( 'url' ),
'twitter:creator' => apply_filters('customer', 'name'),
'twitter:title' => get_bloginfo( 'name' ),
'twitter:description' => prepare_string_for_meta(get_bloginfo('description')),
'twitter:image' => apply_filters('customer', 'default_share_image'),
);
if(is_archive()){
$archive = array(
'og:title' => get_the_archive_title(),
'og:description' => get_the_archive_description()
);
// Merge into $data
$metatags = array_merge($metatags, $archive);
}
if( is_singular() ){
$article = array(
'description' => prepare_string_for_meta(get_field('accroche')),
'article:published_time' => get_the_date( 'c', $post->ID ), // ISO 8601
'article:modified_time' => get_the_modified_date( 'c', $post->ID ), // ISO 8601
'og:title' => strip_tags(get_the_title()),
'og:url' => get_permalink(),
'og:description' => prepare_string_for_meta(get_field('accroche')),
'twitter:title' => prepare_string_for_meta(get_the_title()),
'twitter:description' => prepare_string_for_meta(get_field('accroche')),
);
$cats = array();
$cats = wp_get_post_terms($post->ID, 'category', array("fields" => "names"));
if(!empty($cats)){
$article['article:section'] = implode(',', $cats);
}
$tags = array();
$tags = wp_get_post_terms($post->ID, 'post_tag', array("fields" => "names"));
if(!empty($tags)){
$article['article:tag'] = implode(',', $tags);
}
if(has_post_thumbnail( $post->ID ) ){
$article['og:image'] = get_the_post_thumbnail_url($post->ID);
$article['twitter:image'] = $article['og:image'];
}
// Merge into $data
$metatags = array_merge($metatags, $article);
}
return $metatags;
}
function prepare_string_for_meta($meta, $length = 125) {
/*
Massages WP content (or any string) so that it can be used in html attributes.
usage: sets $length to 0 for no trim.
*/
$meta = strip_tags($meta);
$meta = strip_shortcodes($meta);
$meta = trim(str_replace(array("\n", "\r", "\t"), ' ', $meta));
if($length>0){
$meta = substr($meta, 0, $length);
}
return $meta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment