Skip to content

Instantly share code, notes, and snippets.

@rmarcano
Last active July 1, 2025 17:14
Show Gist options
  • Save rmarcano/33595d42a8e77148ee456a83de71ca4e to your computer and use it in GitHub Desktop.
Save rmarcano/33595d42a8e77148ee456a83de71ca4e to your computer and use it in GitHub Desktop.
Add "IsAccessibleForFree" to the Article schema
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Conditionally sets 'isAccessibleForFree' in Yoast's Article schema based on the 'premium' post tag.
* - If a post has the 'premium' tag, isAccessibleForFree is set to 'false'.
* - For all other posts, isAccessibleForFree is set to 'true'.
*
* Credit: Yoast team
* Last Tested: Jun 24 2025 using Yoast SEO 25.3.1 on WordPress 6.8.1
* Documentation: https://developer.yoast.com/features/schema/pieces/article/#api
*
* NOTE: This function checks for a tag with the SLUG 'premium'. If your tag's slug is different
* (e.g., 'premium-content'), you must update the 'premium' string in the has_tag() function below.
*
* @param array $data The Article schema data.
* @return array The modified schema data.
*/
function add_is_accessible_for_free_to_article_schema( $data ) {
// Get the ID of the current post being processed by Yoast.
$post_id = get_the_ID();
// If we can't determine the post ID, return the original data without adding the property.
if ( ! $post_id ) {
return $data;
}
// Check if the current post has the tag with the slug 'premium'.
if ( has_tag( 'premium', $post_id ) ) {
// If the 'premium' tag exists, the content is behind a paywall (not free).
$data['isAccessibleForFree'] = false;
} else {
// Otherwise, the content is accessible for free.
$data['isAccessibleForFree'] = true;
}
return $data;
}
add_filter( 'wpseo_schema_article', 'add_is_accessible_for_free_to_article_schema' );
/**
* Add the 'isAccessibleForFree' property to Yoast's Article schema.
*
* @param array $data The Article schema data.
* @return array The modified schema data.
*/
function add_is_accessible_for_free_to_article_schema( $data ) {
// Set the property to true. You can change this to false if needed.
$data['isAccessibleForFree'] = true;
return $data;
}
add_filter( 'wpseo_schema_article', 'add_is_accessible_for_free_to_article_schema' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment