Skip to content

Instantly share code, notes, and snippets.

@mrkdevelopment
Last active March 28, 2024 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkdevelopment/0858637df62b43e5ffb3490d33e3768b to your computer and use it in GitHub Desktop.
Save mrkdevelopment/0858637df62b43e5ffb3490d33e3768b to your computer and use it in GitHub Desktop.
Stop access for 'Users' in REST Path for WordPress
<?php
// Check for pluggable function
if ( function_exists( 'is_user_logged_in' ) ) {
if ( ! is_user_logged_in() ) {
add_filter(
'rest_endpoints',
function ( $endpoints ) {
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
});
}
}
<?php
// Remove users from REST
function mrk_remove_rest_users( $endpoints ) {
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
};
// Check for pluggable function
if ( function_exists( 'is_user_logged_in' ) ) {
if ( ! is_user_logged_in() ) {
add_filter('rest_endpoints', 'mrk_remove_rest_users');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment