Dramatically Increase Visibility in Search for WordPress Using JSON-LD Payloads and This PHP Snippet
WordPress runs about 25% of the internet and holds one of the all-time records for adoption. But, managing its search presence is sloppy, difficult, and inexact. JSON-LD and PHP can fix this.
Join the DZone community and get the full member experience.
Join For FreeWordPress may not be ideal. It may not be coherent in terms of its user experience. It may not offer granular enough control. And editing its source files is mostly trouble.
But, it is everywhere and about as difficult to avoid as oxygen. Chances are, you'll be called upon to work with WordPress at some point in your career, because it's not going anywhere soon.
Search Me
SEO is usually managed from within WordPress by a variety of plugins—usually, not just one is sufficient. Most are missing crucial components like the ability to generate DublinCore here or Schema.org information there. (And, we know how well WordPress manages dependencies and conflicts—which is to say, it doesn't.)
This is a pain point for SEOs, who may not necessarily have the knowledge to implement schema.org. Almost no web developer wants to implement any of this microformat or semantic tagging in HTML, by hand. It's messy, and depending on the theme files and whatever else is going on, you may not actually be able to put your information correctly into the "loop."
Implementing in Less Than 100 Lines
Modify header.php
to include:
<?php include('json-ld.php'); ?>
<script type="application/ld+json"><?php echo json_encode($payload); ?></script>
Create json-ld.php
for your theme:
// JSON-LD for Wordpress Home Articles and Author Pages (by Pete Wailes and Richard Baxter)
function get_post_data() {
global $post;
return $post;
}
// Any Page
$payload["@context"] = "http://schema.org/";
// Data of the post/page
$post_data = get_post_data();
// Any page, if it exists
$category = get_the_category();
// Specific Pages
if (is_single()) {
// this gets the data for the user who wrote that particular item
$author_data = get_userdata($post_data->post_author);
$post_url = get_permalink();
$post_thumb = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$payload["@type"] = "Article";
$payload["url"] = $post_url;
$payload["author"] = array(
"@type" => "Person",
"name" => $author_data->display_name,
);
$payload["headline"] = $post_data->post_title;
$payload["datePublished"] = $post_data->post_date;
$payload["image"] = $post_thumb;
$payload["ArticleSection"] = $category[0]->cat_name;
$payload["Publisher"] = "Builtvisible";
}
// we do all this separately so we keep the right things for organization together
if (is_front_page()) {
$payload["@type"] = "Organization";
$payload["name"] = "Builtvisible";
$payload["logo"] = "http://<your domain>.com/wp-content/uploads/logo.png";
$payload["url"] = "http://<your domain>.com/";
$payload["sameAs"] = array(
"https://twitter.com/twits",
"https://www.facebook.com/face",
"https://www.linkedin.com/company/linkedinpark",
);
$payload["contactPoint"] = array(
array(
"@type" => "ContactPoint",
"telephone" => "+8005555555",
"email" => "help@<your domain>.com",
"contactType" => "sales"
)
);
}
if (is_author()) {
// this gets the data for the user who wrote that particular item
$author_data = get_userdata($post_data->post_author);
// some of you may not have all of these data points in your user profiles - delete as appropriate
// fetch twitter from author meta and concatenate with full twitter URL
$twitter_url = " https://twitter.com/";
$twitterHandle = get_the_author_meta('twitter');
$twitterHandleURL = $twitter_url . $twitterHandle;
$websiteHandle = get_the_author_meta('url');
$facebookHandle = get_the_author_meta('facebook');
$gplusHandle = get_the_author_meta('googleplus');
$linkedinHandle = get_the_author_meta('linkedin');
$slideshareHandle = get_the_author_meta('slideshare');
$payload["@type"] = "Person";
$payload["name"] = $author_data->display_name;
$payload["email"] = $author_data->user_email;
$payload["sameAs"] = array(
$twitterHandleURL, $websiteHandle, $facebookHandle, $gplusHandle, $linkedinHandle, $slideshareHandle
);
}
Credits
Read this amazing and expanded discussion from an SEO's point of view:
Published at DZone with permission of Rodrigo Kyle Mehren, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments