wp_unique_prefixed_id( string $prefix = '' ): string
- Since
- 6.4.0
- Source
wp-includes/functions.php:8175
Description
It is similar to wp_unique_id, but each prefix has its own internal ID counter to make each prefix independent from each other. The ID starts at 1 and increments on each call. The returned value is not universally unique, but it is unique across the life of the PHP process and it's stable per prefix.
Compatibility
- WordPress
- since 6.4.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
$prefixstringoptional- Prefix for the returned ID. Default empty string.Default:
''
Return value
string- Incremental ID per prefix.
Performance profile
How much work a call to wp_unique_prefixed_id() 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
- Trivial
- Scaling
- Constant
- Instructions
- 10–21
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
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 21.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wp_unique_prefixed_id()
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_unique_prefixed_id() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_string($prefix) | 10–12 | none |
!is_string($prefix) | 19–21 | wp_trigger_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 21 | 10–21 | 2 | |
| 8.5 | 21 | 10–21 | 2 | |
| 8.4 | 21 | 10–21 | 2 | 1 fewer instruction than PHP 8.3 |
| 8.3 | 22 | 10–22 | 2 | |
| 8.2 | 22 | 10–22 | 2 | |
| 8.1 | 22 | 10–22 | 2 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 23 | 11–23 | 2 |
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.
Uses · 1
- wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
Used by · 3
- Walker_Category_Checklist::start_el()Start the element output.
- img_caption_shortcode()Builds the Caption shortcode output.
- wp_get_elements_class_name()Gets the elements class names.
Source code
function wp_unique_prefixed_id( $prefix = '' ) { static $id_counters = array(); if ( ! is_string( $prefix ) ) { wp_trigger_error( __FUNCTION__, sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) ) ); $prefix = ''; } if ( ! isset( $id_counters[ $prefix ] ) ) { $id_counters[ $prefix ] = 0; } $id = ++$id_counters[ $prefix ]; return $prefix . (string) $id;}Changelog
Introduced in 6.4.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.1.0 tag, from
src/wp-includes/functions.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.