Paywalled Content Structured Data
Marks members-only posts as not free with isAccessibleForFree and the CSS class of the gated section, so Google does not treat the hidden part as cloaking.
Opens WPIDE on
Code
// Categories whose posts are paywalled, and the class of the gated section.
$wpide_paywall_categories = array( 'premium' );
$wpide_paywall_class = 'paywall';
add_action( 'wp_head', function () use ( $wpide_paywall_categories, $wpide_paywall_class ) {
if ( ! is_singular( 'post' ) || ! has_category( $wpide_paywall_categories, get_queried_object_id() ) ) {
return;
}
$post = get_queried_object();
$data = array(
'@context' => 'https://schema.org',
'@type' => 'NewsArticle',
'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 ) ),
'isAccessibleForFree' => false,
'hasPart' => array(
'@type' => 'WebPageElement',
'isAccessibleForFree' => false,
'cssSelector' => '.' . sanitize_html_class( $wpide_paywall_class ),
),
);
echo '<script type="application/ld+json">' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );
Notes
Wrap the members-only part of the content in an element with the paywall class. If your SEO plugin already outputs Article schema, this adds a second, compatible Article node.
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.