Title: wp_unique_prefixed_id
Published: November 8, 2023
Last modified: February 24, 2026

---

# wp_unique_prefixed_id( string $prefix ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#wp--skip-link--target)

Generates an incremental ID that is independent per each different prefix.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#parameters)󠁿

 `$prefix`stringoptional

Prefix for the returned ID. Default empty string.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#return)󠁿

 string Incremental ID per prefix.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L8033)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L8033-L8051)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_trigger_error()](https://developer.wordpress.org/reference/functions/wp_trigger_error/)`wp-includes/functions.php` |

Generates a user-level error/warning/notice/deprecation message.

  |

| Used by | Description | 
| [Walker_Category_Checklist::start_el()](https://developer.wordpress.org/reference/classes/walker_category_checklist/start_el/)`wp-admin/includes/class-walker-category-checklist.php` |

Start the element output.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/?output_format=md#comment-content-6766)
 2.    [Damon Cook](https://profiles.wordpress.org/colorful-tones/)  [  2 years ago  ](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/#comment-6766)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_unique_prefixed_id%2F%23comment-6766)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_unique_prefixed_id%2F%23comment-6766)
 4.  One might ask: what is the difference between `wp_unique_id( 'my-prefix' );` and`
     wp_prefixed_unique_id( 'my-prefix' )`, because they both increment a counter.
 5.  `wp_prefixed_unique_id( 'my-prefix' );` increments based on the matching of the
     actual prefix string: `my-prefix`.
 6.  `wp_unique_id()` will increment regardless of whether you provide a string or 
     not, and it will not attempt to match the string.
 7.  So, another plugin might call `wp_unique_id()` on the page lifecycle and you call
     later on `wp_unique_id( 'my-prefix' )`. Your counter will be `2`.
 8.   * I meant to say `wp_unique_prefixed_id()` and not `wp_prefixed_unique_id()` 
        in all of my examples above. 🤦🏼‍♂️
      * [Damon Cook](https://profiles.wordpress.org/colorful-tones/) [2 years ago](https://developer.wordpress.org/reference/functions/wp_unique_prefixed_id/#comment-6767)
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_unique_prefixed_id%2F%3Freplytocom%3D6766%23feedback-editor-6766)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_unique_prefixed_id%2F)
before being able to contribute a note or feedback.