Last active
March 28, 2024 11:45
-
-
Save mrkdevelopment/0858637df62b43e5ffb3490d33e3768b to your computer and use it in GitHub Desktop.
Stop access for 'Users' in REST Path for WordPress
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 | |
// 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; | |
}); | |
} | |
} |
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 | |
// 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