Last active
August 15, 2023 10:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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