What is JSON-LD?
JSON-LD is a method for representing structured data. Its purpose is to make web pages more semantic, allowing machines to easily read and understand the data on a page.
Search engines can display rich snippets based on JSON-LD data, improving the search experience for users. When we search for recipes, seeing ingredients and preparation steps directly displayed is because this data helps search engines understand the structure of the webpage information.
Adding JSON-LD Structured Data to WordPress via Code
The following code is a snippet this site once used to add structured data. By placing this code in the theme’s functions.php file, we can display this structured data on all pages of the site.
add_action('wp_head', function ()
{
// Open script
$html = '<script type="application/ld+json">';
$html .= '{';
$html .= '"@context": "http://schema.org",';
$html .= '"@type": "WebSite",';
$html .= '"name": "WordPress Knowledge Base",';
$html .= '"alternateName": "WP KB",';
$html .= '"url": "' . home_url() . '"';
$html .= '}';
// Close script
$html .= '</script>';
echo $html;
});
"@context": "http://schema.org"indicates that this is a structured data format defined by schema.org"@type": "WebSite"indicates that this is JSON-LD structured data about site information"name": "WordPress Knowledge Base"indicates that the site name is “WordPress Knowledge Base”"alternateName": "WP KB"is the site’s alias
Since this is a script, ordinary users cannot see it directly, but search engines can. They can easily understand information about this site. For more types and usage methods of structured data, please refer to the official schema.org website.
Adding JSON-LD Structured Data to WordPress via Plugins
Adding JSON-LD data via code is tedious and prone to errors. Friends who aren’t familiar with code can add JSON-LD data to their WordPress sites via plugins. Below are several plugins that can add JSON-LD data to WordPress:
These plugins have similar functions; you can use whichever meets your needs. Additionally, the popular Yoast SEO plugin provides JSON-LD data for site search, site information, site logo, social information, and breadcrumb navigation by default—we can directly enable them.
It’s worth noting that you shouldn’t over-add JSON-LD data for SEO. Only add it when there’s relevant data on the page; otherwise, your site might be penalized by search engines for over-optimization. For example, if you’re a tech site, adding recipe structured data would look very strange.
