WP_Application_Passwords::create_new_application_password( int $user_id, array $args = array() ): array|WP_Error
- Since
- 5.6.0, 5.7.0, 6.8.0
- Source
wp-includes/class-wp-application-passwords.php:89
Compatibility
- WordPress
- since 6.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$user_idint- User ID.
$argsarrayoptional- Arguments used to create the application password.Default:
array()$namestringThe name of the application password.$app_idstringA UUID provided by the application to uniquely identify it.
Return value
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.
Performance profile
How much work a call to WP_Application_Passwords::create_new_application_password() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 15–77
- Plugin surface
- 1 hook
- Called by
- 1
Reads stored settings via get_network_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.
Third-party callbacks on 'wp_create_application_password' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionnetwork option
get_network_option()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_get()one call below WP_Application_Passwords::create_new_application_password() - serializeserialisation
maybe_unserialize()one call below WP_Application_Passwords::create_new_application_password()
Further down the call graph this can also reach transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Application_Passwords::create_new_application_password() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($args) | 15 | __() |
| always | 22 | sanitize_text_field(), __() |
| always | 52–53 | wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), __(), uuid() |
!empty($args) | 59–60 | sanitize_text_field(), wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), __(), uuid() |
get_network_option() | 63–64 | wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), get_main_network_id(), get_network_option(), do_action() |
!get_network_option() | 69–70 | wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), get_main_network_id(), get_network_option(), update_network_option(), do_action() |
!empty($args) && get_network_option() | 70–71 | sanitize_text_field(), wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), get_main_network_id(), get_network_option(), do_action() |
!empty($args) && !get_network_option() | 76–77 | sanitize_text_field(), wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), get_main_network_id(), get_network_option(), update_network_option(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 95 | 15–77 | 5 | |
| 8.5 | 95 | 15–77 | 5 | |
| 8.4 | 95 | 15–77 | 5 | |
| 8.3 | 95 | 15–77 | 5 | |
| 8.2 | 95 | 15–77 | 5 | |
| 8.1 | 95 | 15–77 | 5 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 96 | 15–77 | 5 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Application_Passwords::create_new_application_password() runs, in this order:
- do_action( wp_create_application_password )actionline 150 (+61 into the body)
Fires when an application password is created.
Uses · 12
- sanitize_text_field()Sanitizes a string from user input or from the database.
- __()Retrieves the translation of $text.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- wp_generate_uuid4()Generates a random UUID (version 4).
- get_main_network_id()Gets the main network ID.
- get_network_option()Retrieves a network's option value based on the option name.
- update_network_option()Updates the value of a network option that was already added.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
- WP_Application_Passwords::hash_password()Hashes a plaintext application password.
- static::get_user_application_passwords()
- static::set_user_application_passwords()
Used by · 1
- WP_REST_Application_Passwords_Controller::create_item()Creates an application password.
Source code
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 ); }Changelog
Introduced in 5.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wp-application-passwords.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.