Home > AI > Uncategorized

wordpress REST API

What is REST API

Access web contents via json format according to these links

mydomain.com/wp-json/wp/v2/users

mydomain.com/wp-json/wp/v2/posts

How to disable REST API to protect web contents?

Allow REST API only for register users

function rest_only($result) {
  if ( ! empty( $result ) ) {
    return $result;
  }
  if ( ! is_user_logged_in() ) {
    return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
  }
  return $result;
}
add_filter( 'rest_authentication_errors', rest_only);

Allow REST API only for administrators users

function rest_only($result) {
  if ( ! empty( $result ) ) {
    return $result;
  }
  if ( ! is_user_logged_in() ) {
    return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
  }
  if ( ! current_user_can( 'administrator' ) ) {
    return new WP_Error( 'rest_not_admin', 'You are not an administrator.', array( 'status' => 401 ) );
  }
  return $result;
}
add_filter( 'rest_authentication_errors', rest_only);

How to allow IOS to access web content?

Related posts:

Leave a Reply