PHPwordpressintermediate
Add JSON-LD Schema to WordPress Without Plugins
Inject dynamic JSON-LD structured data into WordPress without any SEO plugins using functions.php
Faisal Yaqoob
December 15, 2025
#wordpress#schema#json-ld#seo#structured-data#rich-snippets#google
Code
php
1 /** 2 * Output JSON-LD Schema Markup for WordPress 3 * Add to your child theme's functions.php 4 */ 5 function wp_custom_json_ld_schema() { 6 $schema = []; 7
8 if ( is_front_page() ) { 9 $schema = wp_get_website_schema(); 10 } elseif ( is_singular( 'post' ) ) { 11 $schema = wp_get_article_schema(); 12 } elseif ( is_singular( 'page' ) ) { 13 $schema = wp_get_webpage_schema(); 14 } elseif ( is_author() ) { 15 $schema = wp_get_person_schema(); 16 } elseif ( is_category() || is_tag() ) { 17 $schema = wp_get_collection_schema(); 18 } 19
20 if ( ! empty( $schema ) ) { 21 echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . '</script>' . "\n"; 22 } 23 } 24 add_action( 'wp_head', 'wp_custom_json_ld_schema' );
Add JSON-LD Schema to WordPress Without Plugins
Inject dynamic Schema.org JSON-LD structured data directly into your WordPress site without relying on heavy SEO plugins. This approach gives you full control over your structured data output and keeps your site lightweight.
Auto-Detect Page Type & Output Schema
/**
* Output JSON-LD Schema Markup for WordPress
* Add to your child theme's functions.php
*/
function wp_custom_json_ld_schema() {
$schema = [];
if ( is_front_page() ) {
$schema = wp_get_website_schema();
} elseif ( is_singular( 'post' ) ) {
$schema = wp_get_article_schema();
} elseif ( is_singular( 'page' ) ) {
$schema = wp_get_webpage_schema();
} elseif ( is_author() ) {
$schema = wp_get_person_schema();
} elseif ( is_category() || is_tag() ) {
$schema = wp_get_collection_schema();
}
if ( ! empty( $schema ) ) {
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . '</script>' . "\n";
}
}
add_action( 'wp_head', 'wp_custom_json_ld_schema' );
WebSite Schema (Homepage)
function wp_get_website_schema() {
return [
'@context' => 'https://schema.org',
'@type' => 'WebSite',
'name' => get_bloginfo( 'name' ),
'description' => get_bloginfo( 'description' ),
'url' => home_url( '/' ),
'publisher' => [
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'logo' => [
'@type' => 'ImageObject',
'url' => get_site_icon_url( 512 ),
],
],
'potentialAction' => [
'@type' => 'SearchAction',
'target' => home_url( '/?s={search_term_string}' ),
'query-input' => 'required name=search_term_string',
],
];
}
Article Schema (Blog Posts)
function wp_get_article_schema() {
global $post;
$author = get_the_author_meta( 'display_name', $post->post_author );
$author_url = get_author_posts_url( $post->post_author );
$thumbnail = get_the_post_thumbnail_url( $post->ID, 'full' );
$categories = wp_get_post_categories( $post->ID, [ 'fields' => 'names' ] );
$tags = wp_get_post_tags( $post->ID, [ 'fields' => 'names' ] );
$schema = [
'@context' => 'https://schema.org',
'@type' => 'Article',
'headline' => get_the_title(),
'description' => wp_trim_words( get_the_excerpt(), 30 ),
'url' => get_permalink(),
'datePublished' => get_the_date( 'c' ),
'dateModified' => get_the_modified_date( 'c' ),
'author' => [
'@type' => 'Person',
'name' => $author,
'url' => $author_url,
],
'publisher' => [
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'logo' => [
'@type' => 'ImageObject',
'url' => get_site_icon_url( 512 ),
],
],
'mainEntityOfPage' => [
'@type' => 'WebPage',
'@id' => get_permalink(),
],
];
if ( $thumbnail ) {
$schema['image'] = $thumbnail;
}
if ( ! empty( $categories ) ) {
$schema['articleSection'] = implode( ', ', $categories );
}
if ( ! empty( $tags ) ) {
$schema['keywords'] = implode( ', ', $tags );
}
// Add word count for article quality signal
$schema['wordCount'] = str_word_count( wp_strip_all_tags( $post->post_content ) );
return $schema;
}
WebPage Schema (Static Pages)
function wp_get_webpage_schema() {
return [
'@context' => 'https://schema.org',
'@type' => 'WebPage',
'name' => get_the_title(),
'description' => wp_trim_words( get_the_excerpt(), 30 ),
'url' => get_permalink(),
'isPartOf' => [
'@type' => 'WebSite',
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
],
'datePublished' => get_the_date( 'c' ),
'dateModified' => get_the_modified_date( 'c' ),
];
}
Person Schema (Author Pages)
function wp_get_person_schema() {
$author = get_queried_object();
return [
'@context' => 'https://schema.org',
'@type' => 'Person',
'name' => $author->display_name,
'description' => get_the_author_meta( 'description', $author->ID ),
'url' => get_author_posts_url( $author->ID ),
'image' => get_avatar_url( $author->ID, [ 'size' => 512 ] ),
'sameAs' => array_filter( [
get_the_author_meta( 'twitter', $author->ID ),
get_the_author_meta( 'linkedin', $author->ID ),
get_the_author_meta( 'github', $author->ID ),
] ),
];
}
CollectionPage Schema (Categories/Tags)
function wp_get_collection_schema() {
$term = get_queried_object();
return [
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => $term->name,
'description' => $term->description ?: 'Browse all posts in ' . $term->name,
'url' => get_term_link( $term ),
'isPartOf' => [
'@type' => 'WebSite',
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
],
];
}
Organization Schema (Site-Wide)
/**
* Add Organization schema to every page
*/
function wp_organization_schema() {
$schema = [
'@context' => 'https://schema.org',
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
'logo' => get_site_icon_url( 512 ),
'description' => get_bloginfo( 'description' ),
'sameAs' => [
'https://www.facebook.com/yourpage',
'https://twitter.com/yourhandle',
'https://www.linkedin.com/company/yourcompany',
],
];
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES ) . '</script>' . "\n";
}
add_action( 'wp_head', 'wp_organization_schema' );
Testing & Validation
# Validate your schema after deployment
# Google Rich Results Test
https://search.google.com/test/rich-results
# Schema.org Validator
https://validator.schema.org/
# Check in Google Search Console
# Navigate to Enhancements > Rich Results
Best Practices
- Use
wp_json_encode()instead ofjson_encode()for WordPress compatibility and security - Add to child theme to survive theme updates
- Use conditional tags (
is_singular(),is_front_page()) for page-specific schema - Include
dateModified— Google prioritizes fresh content signals - Keep schema consistent with visible page content to avoid penalties
- Validate regularly using Google Rich Results Test after any changes
Features
- Zero Plugin Dependencies: Pure PHP, no plugin overhead
- Auto Page Detection: Outputs correct schema per page type
- Dynamic Content: Pulls live data from WordPress database
- Search Action: Enables Google Sitelinks Search Box
- Full Control: Customize every schema property
- Performance: No extra HTTP requests or JavaScript
Related Snippets
FAQ Schema Markup for WordPress Without Plugins
Add FAQPage structured data to WordPress posts and pages for Google rich results without any plugins
PHPwordpressbeginner
phpPreview
/**
* Register FAQ Meta Box for Posts and Pages
*/
function faq_schema_add_meta_box() {
...#wordpress#faq#schema+5
12/18/2025
View
Breadcrumb Schema for WordPress Without Plugins
Add BreadcrumbList JSON-LD structured data to WordPress for enhanced Google search navigation
PHPwordpressbeginner
phpPreview
/**
* Output BreadcrumbList JSON-LD Schema
* Automatically detects page type and builds breadcrumb trail
*/
...#wordpress#breadcrumb#schema+5
12/20/2025
View
Local Business Schema for WordPress
Add LocalBusiness JSON-LD structured data to WordPress for Google Maps and local search rich results
PHPwordpressintermediate
phpPreview
/**
* Output LocalBusiness JSON-LD Schema
* Add to child theme functions.php
* Customize the values below for your business
...#wordpress#local-business#schema+5
12/22/2025
View