WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/06/2019 08:18:08 PM (3 years ago)
Author:
desrosj
Message:

Twenty Seventeen: Use a simple counter incremented with each call instead of uniqid() for generating unique IDs for HTML elements.

Props westonruter, laurelfulford.

Merges [43659] and [44408] to the 5.0 branch.
Fixes #44883.

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-content/themes/twentyseventeen/functions.php

    r44213 r44409  
    587587
    588588/**
     589 * Get unique ID.
     590 *
     591 * This is a PHP implementation of Underscore's uniqueId method. A static variable
     592 * contains an integer that is incremented with each call. This number is returned
     593 * with the optional prefix. As such the returned value is not universally unique,
     594 * but it is unique across the life of the PHP process.
     595 *
     596 * @since Twenty Seventeen 2.0
     597 * @see wp_unique_id() Themes requiring WordPress 5.0.3 and greater should use this instead.
     598 *
     599 * @staticvar int $id_counter
     600 *
     601 * @param string $prefix Prefix for the returned ID.
     602 * @return string Unique ID.
     603 */
     604function twentyseventeen_unique_id( $prefix = '' ) {
     605    static $id_counter = 0;
     606    if ( function_exists( 'wp_unique_id' ) ) {
     607        return wp_unique_id( $prefix );
     608    }
     609    return $prefix . (string) ++$id_counter;
     610}
     611
     612/**
    589613 * Implement the Custom Header feature.
    590614 */
Note: See TracChangeset for help on using the changeset viewer.