A WordPress-centric search engine for devs and theme authors



_wp_filter_build_unique_id ›

Since2.2.3
Deprecatedn/a
_wp_filter_build_unique_id ( $tag, $function, $priority )
Access:
  • private
Parameters: (3)
  • (string) $tag Used in counting how many hooks were applied
    Required: Yes
  • (callable) $function Used for creating unique id
    Required: Yes
  • (int|bool) $priority Used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
    Required: Yes
Links:
Returns:
  • (string|false) Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a unique id.
Defined at:
Codex:

Build Unique ID for storage and retrieval.

The old way to serialize the callback caused issues and this function is the solution. It works by checking for objects and creating a new property in the class to keep track of the object and new objects of the same class that need to be added.

It also allows for the removal of actions and filters for objects after they change class properties. It is possible to include the property $wp_filter_id in your class and set it to "null" or a number to bypass the workaround. However this will prevent you from adding new classes and any new classes will overwrite the previous hook by the same class.

Functions and static method callbacks are just returned as strings and shouldn't have any speed penalty.



Source

function _wp_filter_build_unique_id( $tag, $function, $priority ) {
	global $wp_filter;
	static $filter_id_count = 0;

	if ( is_string( $function ) ) {
		return $function;
	}

	if ( is_object( $function ) ) {
		// Closures are currently implemented as objects
		$function = array( $function, '' );
	} else {
		$function = (array) $function;
	}

	if ( is_object( $function[0] ) ) {
		// Object Class Calling
		if ( function_exists( 'spl_object_hash' ) ) {
			return spl_object_hash( $function[0] ) . $function[1];
		} else {
			$obj_idx = get_class( $function[0] ) . $function[1];
			if ( ! isset( $function[0]->wp_filter_id ) ) {
				if ( false === $priority ) {
					return false;
				}
				$obj_idx                  .= isset( $wp_filter[ $tag ][ $priority ] ) ? count( (array) $wp_filter[ $tag ][ $priority ] ) : $filter_id_count;
				$function[0]->wp_filter_id = $filter_id_count;
				++$filter_id_count;
			} else {
				$obj_idx .= $function[0]->wp_filter_id;
			}

			return $obj_idx;
		}
	} elseif ( is_string( $function[0] ) ) {
		// Static Calling
		return $function[0] . '::' . $function[1];
	}
}