Article Structured Data for Posts
Outputs BlogPosting schema (JSON-LD) on single posts with the headline, dates, author, image and publisher.
Opens WPIDE on
Code
add_action( 'wp_head', function () {
if ( ! is_singular( 'post' ) ) {
return;
}
$post = get_queried_object();
$data = array(
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'headline' => wp_strip_all_tags( get_the_title( $post ) ),
'datePublished' => get_the_date( DATE_W3C, $post ),
'dateModified' => get_the_modified_date( DATE_W3C, $post ),
'mainEntityOfPage' => get_permalink( $post ),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta( 'display_name', $post->post_author ),
'url' => get_author_posts_url( $post->post_author ),
),
'publisher' => array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
),
);
if ( has_post_thumbnail( $post ) ) {
$data['image'] = get_the_post_thumbnail_url( $post, 'full' );
}
echo '<script type="application/ld+json">' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG ) . "</script>\n";
} );
Notes
Skip this snippet if an SEO plugin already outputs article schema.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.