Last active
October 11, 2018 15:15
-
-
Save larodiel/33ba1f7e8878fe63f47dc5b875063a9b to your computer and use it in GitHub Desktop.
Article Structured Data Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function json_ld_article() { | |
if(is_single()) { | |
global $post; | |
$user_name = get_bloginfo('name'); | |
$article_title = get_the_title(); | |
$excerpt = get_the_excerpt(); | |
$post_author_id = (int)get_post_field( 'post_author', $post->ID ); | |
if( get_the_author_meta('display_name', $post_author_id ) != "" ) { | |
$user_name = get_the_author_meta('display_name', $post_author_id ); | |
} | |
else if( get_the_author_meta('user_nicename', $post_author_id) != "" ) { | |
$user_name = get_the_author_meta('user_nicename', $post_author_id); | |
} | |
else if( get_the_author_meta('first_name', $post_author_id) != "") { | |
$user_name = get_the_author_meta('first_name', $post_author_id); | |
} | |
else { | |
$user_name = get_bloginfo('name'); | |
} | |
if(!has_excerpt() || (trim($excerpt) == '...' || trim($excerpt) == '<p>…</p>' || trim($excerpt) == '<p>...</p>')) { | |
$excerpt = cd_variable_length_excerpt(get_the_content()); | |
} | |
$home_url = get_home_url('/'); | |
$excerpt = str_replace(array("\n", "\r"), '', json_encode(wp_strip_all_tags(trim($excerpt) ))); | |
$category_detail = get_the_category(); | |
$category_name = $category_detail[0]->cat_name; | |
$comments_count = wp_count_comments(); | |
$thumbnailURL = get_the_post_thumbnail_url( get_the_ID(), 'large' ); | |
$post_url = get_the_permalink(); | |
$post_date = get_the_date('c'); | |
$modified_date = get_the_modified_date('c'); | |
$logo = '//placehold.it/512x512'; | |
echo '<meta property="article:author" content="'.$user_name.'" />'; | |
$json_ld = '<script type="application/ld+json">'; | |
$json_ld .= '{'; | |
$json_ld .= '"@context": "http://schema.org",'; | |
$json_ld .= '"@type": "Article",'; | |
$json_ld .= ' "mainEntityOfPage": {'; | |
$json_ld .= ' "@type": "WebPage",'; | |
$json_ld .= ' "@id": "'.$home_url.'"'; | |
$json_ld .= '},'; | |
$json_ld .= '"author": "'.$user_name.'",'; | |
$json_ld .= ' "publisher": {'; | |
$json_ld .= ' "@type": "Organization",'; | |
$json_ld .= ' "name": "'.$user_name.'",'; | |
$json_ld .= ' "@id": "#person",'; | |
$json_ld .= ' "url": "'.$home_url.'",'; | |
$json_ld .= ' "sameAs":[ '; | |
$json_ld .= ' "https:\/\/www.facebook.com\/user",'; | |
$json_ld .= ' "https:\/\/www.instagram.com\/user\/",'; | |
$json_ld .= ' "https:\/\/www.pinterest.com\/user\/",'; | |
$json_ld .= ' "https:\/\/twitter.com\/user"'; | |
$json_ld .= ' ],'; | |
$json_ld .= ' "logo": "'.$logo.'"'; | |
$json_ld .= '},'; | |
$json_ld .= '"name": "'.$article_title.'",'; | |
$json_ld .= '"headline": "'.$article_title.'",'; | |
$json_ld .= '"articleBody": '.$excerpt.','; | |
$json_ld .= '"articleSection": "'.$category_name.'",'; | |
$json_ld .= '"commentCount": '.(int)$comments_count.','; | |
$json_ld .= '"thumbnailUrl": "'.$thumbnailURL.'",'; | |
$json_ld .= '"image": "'.$thumbnailURL.'",'; | |
$json_ld .= '"url": "'.$post_url.'",'; | |
$json_ld .= '"datePublished": "'.$post_date.'",'; | |
$json_ld .= '"dateModified": "'.$modified_date.'"'; | |
$json_ld .= '}'; | |
$json_ld .= '</script>'; | |
echo $json_ld; | |
} | |
} | |
add_action('wp_head', 'json_ld_article'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment