Schema JSON-LD markup for linking Data in a Website has gained lots of popularity for the past couple years, primarily because its is the recommended and preferred structured data mark-up prescribed by none other than Google Developers. In this article, I have published a code, which can enable anybody, including lay-persons, not well-versed with coding to add Schema JSON-LD Blogposting vocabulary for articles to their WordPress Blogs within 5 minutes without the use of any resource consuming free or paid Rich Snippets Mark-UP WordPress-Plugins.
Even though there are scores of articles and tutorials available on the internet, giving examples of JSON-LD mark-up and other Semantic Microdata and Microformat Structured Data/Rich Snippets Mark-Up for Blogs, I could not find any particular tutorial satisfactory because there was no ready-made code, which could be easily copied to your Blog.
The essential and requires properties needed for Google Structered Data Tool validation like mainEntryofPage, headline, Date-Published-Modified, Author Mark-Up, Publisher Logo etc. are met using the code.
//For articlesJSON-LD
add_action(‘wp_head’,’insert_json_ld_article’);
function insert_json_ld_article (){
if (is_single()) {
if (have_posts()) : while (have_posts()) : the_post();//Configuration
$context = ‘http://schema.org’;
$type = ‘BlogPosting’;
$name = get_the_title();
$authorType = ‘Person’;
$authorName = get_the_author();
$dataPublished = get_the_date(‘Y-n-j’);
$dateModified = get_the_modified_time(‘Y-n-j’);
$thumbnail_id = get_post_thumbnail_id($post->ID);
// ↓ Please set it yourself
$imageLogo = ‘https://www.royalchef.info/wp-content/uploads/2016/12/Royalchef-Sujata-e1481865798965.jpg’;
$category_info = get_the_category();
$articleSection = $category_info[0]->name;
$articleBody = get_the_content();
$url = get_permalink();
$publisherType = ‘Organization’;
$publisherName = get_bloginfo(‘name’);$image = wp_get_attachment_image_src( $thumbnail_id, ‘full’ );
$imageurl = $image[0];
// When the thumbnail can not be taken, set the first picture in the sentence
if(!$imageurl){
$imageurl = catch_that_image();
}
// If the first image in the sentence could not be taken, set the logo set above
if(!$imageurl){
$imageurl = $imageLogo;
}// Get the size of the image
$imginfo = getimagesize($imageurl);
$imagewidth = $imginfo[0];
$imageheight = $imginfo[1];// Obtain the size of the logo image
$imginfoLogo = getimagesize($imageLogo);
$imagewidthLogo = $imginfoLogo[0];
$imageheightLogo = $imginfoLogo[1];$json= ”
\”@context\” : \”{$context}\”,
\”@type\” : \”{$type}\”,
\”mainEntityOfPage\” : {
\”@type\” : \”WebPage\”,
\”url\” : \”{$url}\”
},
\”headline\” : \”{$name}\”,
\”author\” : {
\”@type\” : \”{$authorType}\”,
\”name\” : \”{$authorName}\”
},
\”datePublished\” : \”{$dataPublished}\”,
\”dateModified\” : \”{$dateModified}\”,
\”image\” : {
\”@type\” : \”imageObject\”,
\”url\” : \”{$imageurl}\”,
\”width\” : {$imagewidth},
\”height\” : {$imageheight}
},
\”articleSection\” : \”{$articleSection}\”,
\”publisher\” : {
\”@type\” : \”{$publisherType}\”,
\”name\” : \”{$publisherName}\”,
\”logo\” : {
\”@type\” : \”imageObject\”,
\”url\” : \”{$imageLogo}\”,
\”width\” : {$imagewidthLogo},
\”height\” : {$imageheightLogo}
}
}
“;echo ‘<script type=”application/ld+json”>{‘.$json.’}</script>’;
endwhile; endif;
rewind_posts();
}
}function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches);
$first_img = $matches[1][0];return $first_img;
}
The mark-up should be inserted in the function.php file, I have added it before the closing ?>. Replace the logo-url in bold letters with your Logo url. I have included a 80px × 60px for my site, which does not show any validation errors in the Google Mark-Up tool.