How to Add Schema.org Support to WordPress Articles Easily
Explanation
Adding schema.org markup to your WordPress articles helps search engines understand your content better, which can improve your visibility in search results. Here's a simple way to do it using a bit of code.
This code snippet hooks into the WordPress head section to insert schema.org markup for your articles. It checks if the current page is a single post and then gathers all the necessary information about the post, like the title, URL, publication date, and author details.
Key Elements:
- @context: This tells search engines that you're using schema.org vocabulary.
- @type: Specifies that the content is an "Article".
- mainEntityOfPage: Links the article to its webpage URL.
- headline: The title of your article.
- datePublished and dateModified: When the article was published and last modified.
- author: Information about the article's author.
- publisher: Details about the organization publishing the article, including its logo.
- image: The URL of the article's featured image.
This JSON-LD script is then outputted in the head section of your WordPress site, making it easy for search engines to pick up and use.
Code
<?php
// Hook into the 'wp_head' action to add schema.org markup to WordPress articles
add_action('wp_head', 'wp_dudecom_add_article_schema');
function wp_dudecom_add_article_schema() {
if (is_single() && 'post' === get_post_type()) {
global $post;
// Get the post data
$post_id = $post->ID;
$post_title = get_the_title($post_id);
$post_url = get_permalink($post_id);
$post_date = get_the_date('c', $post_id);
$post_modified_date = get_the_modified_date('c', $post_id);
$post_author_id = $post->post_author;
$post_author_name = get_the_author_meta('display_name', $post_author_id);
$post_thumbnail_url = get_the_post_thumbnail_url($post_id, 'full');
// Prepare the schema.org JSON-LD data
$schema_data = array(
'@context' => 'https://schema.org',
'@type' => 'Article',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $post_url
),
'headline' => $post_title,
'datePublished' => $post_date,
'dateModified' => $post_modified_date,
'author' => array(
'@type' => 'Person',
'name' => $post_author_name
),
'publisher' => array(
'@type' => 'Organization',
'name' => get_bloginfo('name'),
'logo' => array(
'@type' => 'ImageObject',
'url' => get_site_icon_url()
)
),
'image' => array(
'@type' => 'ImageObject',
'url' => $post_thumbnail_url
)
);
// Output the schema.org JSON-LD script
echo '<script type="application/ld+json">' . wp_json_encode($schema_data) . '</script>';
}
}
?>
Instructions
File Location: Add the code to your theme's functions.php
file or a custom plugin file.
Prerequisites:
- Ensure you have access to your WordPress theme files or the ability to create/edit a custom plugin.
- Backup your site before making changes to the code.
Implementation Steps:
- Log in to your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor if you are adding the code to
functions.php
. Alternatively, go to Plugins > Add New > Create a new plugin if you prefer using a custom plugin. - In the Theme Editor, locate and select
functions.php
from the list of theme files on the right. If using a plugin, create a new file with a.php
extension. - Copy and paste the provided code snippet into the file.
- Save the changes by clicking the Update File button if editing
functions.php
, or save the file if using a plugin. - Visit a single post on your website and view the page source to ensure the schema.org JSON-LD script is correctly added in the
<head>
section.
If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.