Using WordPress ‘rest_application_password_check_errors()’ PHP function

The rest_application_password_check_errors() WordPress PHP function checks for errors when using application password-based authentication.

Usage

$result = rest_application_password_check_errors($result);

Parameters

  • $result (WP_Error|null|true): Required. Error from another authentication handler, null if we should handle it, or another value if not.

More information

See WordPress Developer Resources: rest_application_password_check_errors

Examples

Basic error checking with application password

// In this example, we'll check if there are any errors when using application password authentication.

add_filter('rest_authentication_errors', 'check_application_password_errors', 10, 1);

function check_application_password_errors($result) {
  if (is_wp_error($result)) {
    $result = **rest_application_password_check_errors**($result);
  }
  return $result;
}

Handling authentication error

// In this example, we'll handle an authentication error when using application password authentication.

add_filter('rest_authentication_errors', 'handle_authentication_error', 10, 1);

function handle_authentication_error($result) {
  if (is_wp_error($result)) {
    $result = **rest_application_password_check_errors**($result);
    if (is_wp_error($result)) {
      // Handle the error, e.g., show a custom error message
      $result->add('custom_error', 'There was an error with application password authentication.');
    }
  }
  return $result;
}

Customizing authentication error message

// In this example, we'll customize the error message for application password authentication.

add_filter('rest_authentication_errors', 'customize_authentication_error', 10, 1);

function customize_authentication_error($result) {
  if (is_wp_error($result)) {
    $result = **rest_application_password_check_errors**($result);
    if (is_wp_error($result)) {
      // Remove the default error message
      $result->remove('rest_application_password_invalid');
      // Add a custom error message
      $result->add('custom_error', 'Please check your application password and try again.');
    }
  }
  return $result;
}

Bypassing application password error

// In this example, we'll bypass the application password error for specific user roles.

add_filter('rest_authentication_errors', 'bypass_application_password_error', 10, 1);

function bypass_application_password_error($result) {
  if (is_wp_error($result)) {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
      // Bypass the error for administrators
      $result = null;
    } else {
      $result = **rest_application_password_check_errors**($result);
    }
  }
  return $result;
}

Logging application password errors

// In this example, we'll log application password errors to a custom log file.

add_filter('rest_authentication_errors', 'log_application_password_errors', 10, 1);

function log_application_password_errors($result) {
  if (is_wp_error($result)) {
    $result = **rest_application_password_check_errors**($result);
    if (is_wp_error($result)) {
      // Log the error to a custom log file
      error_log('Application password error: ' . $result->get_error_message(), 3, 'app_password_errors.log');
    }
  }
  return $result;
}