Creates a new application password.
Parameters
$user_idintrequired- User ID.
$argsarrayoptional- Arguments used to create the application password.
namestringThe name of the application password.app_idstringA UUID provided by the application to uniquely identify it.
Default:
array()
Return
array|WP_Error Application password details, or a WP_Error instance if an error occurs.0stringThe generated application password in plain text.1arrayThe details about the created password.uuidstringThe unique identifier for the application password.app_idstringA UUID provided by the application to uniquely identify it.namestringThe name of the application password.passwordstringA one-way hash of the password.createdintUnix timestamp of when the password was created.last_usednullNull.last_ipnullNull.
Source
public static function create_new_application_password( $user_id, $args = array() ) { if ( ! empty( $args['name'] ) ) { $args['name'] = sanitize_text_field( $args['name'] ); } if ( empty( $args['name'] ) ) { return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ), array( 'status' => 400 ) ); } $new_password = wp_generate_password( static::PW_LENGTH, false ); $hashed_password = self::hash_password( $new_password ); $new_item = array( 'uuid' => wp_generate_uuid4(), 'app_id' => empty( $args['app_id'] ) ? '' : $args['app_id'], 'name' => $args['name'], 'password' => $hashed_password, 'created' => time(), 'last_used' => null, 'last_ip' => null, ); $passwords = static::get_user_application_passwords( $user_id ); $passwords[] = $new_item; $saved = static::set_user_application_passwords( $user_id, $passwords ); if ( ! $saved ) { return new WP_Error( 'db_error', __( 'Could not save application password.' ) ); } $network_id = get_main_network_id(); if ( ! get_network_option( $network_id, self::OPTION_KEY_IN_USE ) ) { update_network_option( $network_id, self::OPTION_KEY_IN_USE, true ); } /** * Fires when an application password is created. * * @since 5.6.0 * @since 6.8.0 The hashed password value now uses wp_fast_hash() instead of phpass. * * @param int $user_id The user ID. * @param array $new_item { * The details about the created password. * * @type string $uuid The unique identifier for the application password. * @type string $app_id A UUID provided by the application to uniquely identify it. * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type null $last_used Null. * @type null $last_ip Null. * } * @param string $new_password The generated application password in plain text. * @param array $args { * Arguments used to create the application password. * * @type string $name The name of the application password. * @type string $app_id A UUID provided by the application to uniquely identify it. * } */ do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args ); return array( $new_password, $new_item ); }Hooks
- do_action( ‘wp_create_application_password’,
int $user_id ,array $new_item ,string $new_password ,array $args ) Fires when an application password is created.
Related
Show 4 moreShow lessUses Description WP_Application_Passwords::hash_password() wp-includes/class-wp-application-passwords.phpHashes a plaintext application password.
wp_generate_uuid4() wp-includes/functions.phpGenerates a random UUID (version 4).
update_network_option() wp-includes/option.phpUpdates the value of a network option that was already added.
get_network_option() wp-includes/option.phpRetrieves a network’s option value based on the option name.
get_main_network_id() wp-includes/functions.phpGets the main network ID.
wp_generate_password() wp-includes/pluggable.phpGenerates a random password drawn from the defined set of characters.
sanitize_text_field() wp-includes/formatting.phpSanitizes a string from user input or from the database.
do_action() wp-includes/plugin.phpCalls the callback functions that have been added to an action hook.
WP_Error::__construct() wp-includes/class-wp-error.phpInitializes the error.
Used by Description WP_REST_Application_Passwords_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.phpCreates an application password.
Changelog
Version Description 6.8.0 The hashed password value now uses wp_fast_hash() instead of phpass. 5.7.0 Returns WP_Error if application name already exists. 5.6.0 Introduced. User Contributed Notes
You must log in before being able to contribute a note or feedback.
Basic Usage
Generate the application password for the logged-in users if not already exists.