Content Restriction but allow public REST API
You can use the REST_REQUEST
constant to detect if this is a REST request, and if it’s not, apply your filter(s).
Example:
add_filter( 'the_content', 'wpse418531_use_excerpt' );
/**
* Replaces the content with the excerpt, unless this is a REST request.
*
* @param string $content The post content.
* @return string The filtered post content.
*/
function wpse418531_use_excerpt( $content ) {
if ( is_admin() ) {
// If this is an admin page, returns the content unchanged.
return $content;
}
if ( defined( REST_REQUEST ) && true === REST_REQUEST ) {
// If this is a REST request, returns the content unchanged.
return $content;
}
$content = get_the_excerpt();
// If you want to add a note about your app, you can do it here.
// If not, delete these lines.
$content .= '<p>See more on <a href="https://example.com/download/my/app/">my app</a>.</p>';
return $content;
}
Note: This code is untested. Don’t use it on a production site till you’ve ensured it works properly. It’s also meant as a starting point for you, not a finished product.
References
rest_api_loaded()
which containsREST_REQUEST
(thanks to this answer for the info)the_content
get_the_excerpt()