Skip to content

Instantly share code, notes, and snippets.

@tommyferry
Last active August 15, 2023 10:57
Show Gist options
  • Save tommyferry/f98e1607eb196cb54930fffc41f596ac to your computer and use it in GitHub Desktop.
Save tommyferry/f98e1607eb196cb54930fffc41f596ac to your computer and use it in GitHub Desktop.
A PHP snippet to protect paywalled content (e.g. whitepapers) from being freely accessed by the WordPress REST API
<?php
function tf_filter_gutenberg_post_types( $args, $post_type ) {
// Return early - we're not registering the right CPT
if ( 'whitepaper' !== $post_type ) {
return $args;
}
// Only enable the REST API for users with edit permissions, otherwise
// explicitly disable it. This prevents other users having access to
// post content (via the API), which would bypass a paywall.
$args['show_in_rest'] = current_user_can( 'edit_whitepapers' );
return $args;
}
add_filter( 'register_post_type_args', 'tf_filter_gutenberg_post_types', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment