Using WordPress ‘rest_application_password_collect_status()’ PHP function

The rest_application_password_collect_status() WordPress PHP function collects the status of authenticating with an application password.

Usage

$status = rest_application_password_collect_status($user_or_error, $app_password = array());

Parameters

  • $user_or_error (WP_Error): Required. The authenticated user or error instance.
  • $app_password (array): Optional. The Application Password used to authenticate. Default: array()

More information

See WordPress Developer Resources: rest_application_password_collect_status

Examples

Collect authentication status

Collects the authentication status using rest_application_password_collect_status() function.

// Assume we have a user or an error instance
$user_or_error = new WP_Error('error_code', 'Error Message');

// Assume we have an application password
$app_password = array(
    'password' => 'your_application_password'
);

// Collect the authentication status
$status = rest_application_password_collect_status($user_or_error, $app_password);

Display authentication status

Displays the authentication status by checking the collected status.

$status = rest_application_password_collect_status($user_or_error, $app_password);

// Check if authentication was successful
if (is_wp_error($status)) {
    echo "Authentication failed: " . $status->get_error_message();
} else {
    echo "Authentication successful!";
}

Log authentication status

Logs the authentication status in a custom log file.

$status = rest_application_password_collect_status($user_or_error, $app_password);

$log_message = is_wp_error($status) ? 'Failed: ' . $status->get_error_message() : 'Successful';

error_log("Application password authentication: " . $log_message, 3, "/path/to/your/logfile.log");

Perform an action upon successful authentication

Performs a custom action when the authentication is successful.

$status = rest_application_password_collect_status($user_or_error, $app_password);

if (!is_wp_error($status)) {
    // Perform your custom action here
    do_action('your_custom_action');
}

Validate application password

Validates the application password by comparing it with a stored password.

// Assume we have a stored password
$stored_password = 'your_stored_password';

$status = rest_application_password_collect_status($user_or_error, $app_password);

if (!is_wp_error($status) && $app_password['password'] === $stored_password) {
    echo "Application password is valid!";
} else {
    echo "Invalid application password!";
}