Yoast SEO is one of our favorite plugins for WordPress. It provides a very simple way to manage and control your SEO with very little effort. They’ve covered all of our needs – until recently.

For years, Google has required users to show at least one free page when showing paywall content. Recently, they have changed the rules to allow Flexible Sampling. Flexible sampling allows you to show only part of the content but they want you to add schema to announce what you’re doing. Regretfully, this is not something that Yoast SEO is currently supporting. They have announced in a support thread that this is a feature they are aware of. Here is a support members quote:

Thanks for bringing this up. We have a feature request to have this added as an option in Yoast SEO for our developers to review, but we can’t give you an estimated date or say whether this will be available in an upcoming release.

Yoast SEO Paywall code

Below is a code that adds paywall schema to your website. It fully integrates using Yoast SEO’s Schema documentation API. Simply add this code to your function.php file.
add_filter( 'wpseo_schema_graph_pieces', 'add_custom_schema_piece_to_give_categories_posts', 11, 2 );
/**
 * Adds a custom graph piece of WebPageElement to the schema collector.
 *
 * @param array  $pieces  The current graph pieces.
 * @param string $context The current context.
 *
 * @return array The graph pieces.
 */
function add_custom_schema_piece_to_give_categories_posts( $pieces, $context ) {
    if ( !is_single()) {
        return $pieces;
    }
   $pieces[] = new WebPageElementSchema();
   return $pieces;
}
class WebPageElementSchema{
	/**
	 * Determines whether or not a piece should be added to the graph.
	 *
	 * @return bool
	 */
	public function is_needed() {
		$cats = get_the_category();
        $given_list = array(46, 47, 48, 49, 50, 51, 52, 53, 54, 55); //edit this line with categories you'd like the code to appear on
        if($cats){
            foreach($cats as $cat){
                if(in_array($cat->term_id, $given_list)){
                    return true;
                }
            }
        }
        return false;
	}
	/**
	 * Returns WebPageElement schema data.
	 *
	 * @return array WebPageElement schema data.
	 */
	public function generate() {
		$data = [
			'@type' => 'WebPageElement',
			'isAccessibleForFree' => 'False',
			'cssSelector' => '.paywall',
        ];
		return $data;
	}
}

Final Step

Now that we have the Schema added, we need to tell google what CSS selector is the paywall message. In this case, we’re going to use the .paywall selector.

<body> <p>This content is outside a paywall and is visible to all.</p> <div class="paywall">This content is inside a paywall, and requires a subscription or registration.</div> </body>

Final Thoughts

We still love Yoast SEO and recommend it highly. We know that paywall content is super niche so we’re not surprised they haven’t added PayWall schema yet and we won’t be surprised if it takes them a while to add it. Until then, let us know if you have any questions on the code we have for you!

2 thoughts on “Add subscription and paywall schema to Yoast SEO

  1. Gino says:

    What is the “//edit this line with categories you’d like the code to appear on
    if($cats){” ?

    I’m using woo commerce paywall, how to implement this for woo commerce membership?

    • OwlManage says:

      Great question! In the example, there are the numbers 46, 47, 48, 49, 50, 51, 52, 53, 54, 55. You would delete these numbers and add the category or page ids where you want the schema to show up.

      The code should work for any plugin. You’ll just want to add the id numbers to the code that is associated with the pay wall pages and categories.

Leave a Reply

Your email address will not be published. Required fields are marked *