# elementor/4.3.0-beta3/modules/atomic-widgets/utils/format-element-ids.php

Elementor Website Builder – more than just a page builder, version 4.3.0-beta3. 53 lines.

- Page: https://pluginprobe.com/plugins/elementor/4.3.0-beta3/code/modules/atomic-widgets/utils/format-element-ids.php
- Raw: https://pluginprobe.com/plugins/elementor/4.3.0-beta3/raw/modules/atomic-widgets/utils/format-element-ids.php
- Modified: 2026-09-01T11:47:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/elementor/4.3.0-beta3/code/modules/atomic-widgets/utils/format-element-ids.php#L10-L20`.

```php
<?php

namespace Elementor\Modules\AtomicWidgets\Utils;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Format_Element_Ids {
	public static function format( array $elements, array $path ): array {
		return array_map( function( $element ) use ( $path ) {
			$origin_id   = $element['id'];
			$nesting_path = [ ...$path, $origin_id ];

			$element['id']        = self::hash_string( implode( '_', $nesting_path ), 7 );
			$element['origin_id'] = $origin_id;
			$element['elements']  = self::format( $element['elements'] ?? [], $nesting_path );

			return $element;
		}, $elements );
	}

	/**
	 * Deterministic djb2-based hash kept in sync with the TypeScript implementation
	 * in @elementor/utils (packages/packages/libs/utils/src/hash.ts) so that inner
	 * element ids are consistent between the PHP render and the editor's JS layer.
	 *
	 * @param string   $str    String to hash.
	 * @param int|null $length Optional desired output length.
	 * @return string          Base-36, lowercase, padded to $length when provided.
	 */
	public static function hash_string( string $str, ?int $length ): string {
		$hash_basis = 5381;

		$i = strlen( $str );
		while ( $i > 0 ) {
			--$i;
			$hash_basis = ( $hash_basis * 33 ) ^ ord( $str[ $i ] );
			// Keep hash within 32-bit range to match JavaScript bitwise operations.
			$hash_basis = $hash_basis & 0xFFFFFFFF;
		}

		$result = base_convert( (string) $hash_basis, 10, 36 );

		if ( ! isset( $length ) ) {
			return $result;
		}

		$sliced = substr( $result, -$length );
		return str_pad( $sliced, $length, '0', STR_PAD_LEFT );
	}
}

```
