wppaste
WordPress

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
Creates a new application password.

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()
  • $namestring

    The name of the application password.
  • $app_idstring

    A 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.
  • $0string

    The generated application password in plain text.
  • $1array

    The details about the created password.
    • $uuidstring

      The unique identifier for the application password.
    • $app_idstring

      A UUID provided by the application to uniquely identify it.
    • $namestring

      The name of the application password.
    • $passwordstring

      A one-way hash of the password.
    • $createdint

      Unix timestamp of when the password was created.
    • $last_usednull

      Null.
    • $last_ipnull

      Null.

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

Reads stored settings via get_network_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–77

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.

Plugin surface
1 hook

Third-party callbacks on 'wp_create_application_password' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionnetwork optionget_network_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_get()one call below WP_Application_Passwords::create_new_application_password()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
empty($args)15__()
always22sanitize_text_field(), __()
always52–53wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), __(), uuid()
!empty($args)59–60sanitize_text_field(), wp_generate_password(), ::hash_password(), wp_generate_uuid4(), time(), ::get_user_application_passwords(), ::set_user_application_passwords(), __(), uuid()
get_network_option()63–64wp_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–70wp_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–71sanitize_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–77sanitize_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

PHPCompiledExecutedBranchesNotes
8.6-dev9515–775
8.59515–775
8.49515–775
8.39515–775
8.29515–775
8.19515–7751 fewer instruction than PHP 7.4
7.49615–775

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:

  1. do_action( wp_create_application_password )actionline 150 (+61 into the body)

    Fires when an application password is created.

Uses · 12

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.8.0
The hashed password value now uses wp_fast_hash() instead of phpass.from the docblock
5.7.0
Returns WP_Error if application name already exists.from the docblock
5.6.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.