_wp_filter_build_unique_id

Build Unique ID for storage and retrieval.

Description

(string|false) _wp_filter_build_unique_id( (string) $tag, (callable) $function, (int|bool) $priority ); 

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.

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.

Parameters (3)

0. $tag (string)
Used in counting how many hooks were applied
1. $function (callable)
Used for creating unique id
2. $priority (int|bool)
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.

Usage

  1. if ( !function_exists( '_wp_filter_build_unique_id' ) ) { 
  2. require_once ABSPATH . WPINC . '/plugin.php'; 
  3.  
  4. // Used in counting how many hooks were applied 
  5. $tag = ''; 
  6.  
  7. // Used for creating unique id 
  8. $function = null; 
  9. $priority = null; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = _wp_filter_build_unique_id($tag, $function, $priority); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/plugin.php  
  1. function _wp_filter_build_unique_id($tag, $function, $priority) { 
  2. global $wp_filter; 
  3. static $filter_id_count = 0; 
  4.  
  5. if ( is_string($function) ) 
  6. return $function; 
  7.  
  8. if ( is_object($function) ) { 
  9. // Closures are currently implemented as objects 
  10. $function = array( $function, '' ); 
  11. } else { 
  12. $function = (array) $function; 
  13.  
  14. if (is_object($function[0]) ) { 
  15. // Object Class Calling 
  16. if ( function_exists('spl_object_hash') ) { 
  17. return spl_object_hash($function[0]) . $function[1]; 
  18. } else { 
  19. $obj_idx = get_class($function[0]).$function[1]; 
  20. if ( !isset($function[0]->wp_filter_id) ) { 
  21. if ( false === $priority ) 
  22. return false; 
  23. $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count; 
  24. $function[0]->wp_filter_id = $filter_id_count; 
  25. ++$filter_id_count; 
  26. } else { 
  27. $obj_idx .= $function[0]->wp_filter_id; 
  28.  
  29. return $obj_idx; 
  30. } elseif ( is_string( $function[0] ) ) { 
  31. // Static Calling 
  32. return $function[0] . '::' . $function[1];