Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.