3 * The plugin API is located in this file, which allows for creating actions
4 * and filters and hooking functions, and methods. The functions or methods will
5 * then be run when the action or filter is called.
7 * The API callback examples reference functions, but can be methods of classes.
8 * To hook methods, you'll need to pass an array one of two ways.
10 * Any of the syntaxes explained in the PHP documentation for the
11 * {@link http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}
14 * Also see the {@link https://codex.wordpress.org/Plugin_API Plugin API} for
15 * more information and examples on how to use a lot of these functions.
22 // Initialize the filter globals.
23 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
25 if ( ! isset( $wp_filter ) )
28 if ( ! isset( $wp_actions ) )
29 $wp_actions = array();
31 if ( ! isset( $merged_filters ) )
32 $merged_filters = array();
34 if ( ! isset( $wp_current_filter ) )
35 $wp_current_filter = array();
38 * Hook a function or method to a specific filter action.
40 * WordPress offers filter hooks to allow plugins to modify
41 * various types of internal data at runtime.
43 * A plugin can modify data by binding a callback to a filter hook. When the filter
44 * is later applied, each bound callback is run in order of priority, and given
45 * the opportunity to modify a value by returning a new value.
47 * The following example shows how a callback function is bound to a filter hook.
49 * Note that `$example` is passed to the callback, (maybe) modified, then returned:
51 * function example_callback( $example ) {
52 * // Maybe modify $example in some way.
55 * add_filter( 'example_filter', 'example_callback' );
57 * Since WordPress 1.5.1, bound callbacks can take as many arguments as are
58 * passed as parameters in the corresponding apply_filters() call. The `$accepted_args`
59 * parameter allows for calling functions only when the number of args match.
61 * *Note:* the function will return true whether or not the callback is valid.
62 * It is up to you to take care. This is done for optimization purposes,
63 * so everything is as quick as possible.
67 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
68 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added,
69 * it doesn't need to run through that process.
71 * @param string $tag The name of the filter to hook the $function_to_add callback to.
72 * @param callback $function_to_add The callback to be run when the filter is applied.
73 * @param int $priority Optional. Used to specify the order in which the functions
74 * associated with a particular action are executed. Default 10.
75 * Lower numbers correspond with earlier execution,
76 * and functions with the same priority are executed
77 * in the order in which they were added to the action.
78 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
79 * @return boolean true
81 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
82 global $wp_filter, $merged_filters;
84 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
85 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
86 unset( $merged_filters[ $tag ] );
91 * Check if any filter has been registered for a hook.
95 * @global array $wp_filter Stores all of the filters.
97 * @param string $tag The name of the filter hook.
98 * @param callback|bool $function_to_check Optional. The callback to check for. Default false.
99 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has
100 * anything registered. When checking a specific function, the priority of that
101 * hook is returned, or false if the function is not attached. When using the
102 * $function_to_check argument, this function may return a non-boolean value
103 * that evaluates to false (e.g.) 0, so use the === operator for testing the
106 function has_filter($tag, $function_to_check = false) {
107 // Don't reset the internal array pointer
108 $wp_filter = $GLOBALS['wp_filter'];
110 $has = ! empty( $wp_filter[ $tag ] );
112 // Make sure at least one priority has a filter callback
115 foreach ( $wp_filter[ $tag ] as $callbacks ) {
116 if ( ! empty( $callbacks ) ) {
127 if ( false === $function_to_check || false == $has )
130 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
133 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
134 if ( isset($wp_filter[$tag][$priority][$idx]) )
142 * Call the functions added to a filter hook.
144 * The callback functions attached to filter hook $tag are invoked by calling
145 * this function. This function can be used to create a new filter hook by
146 * simply calling this function with the name of the new hook specified using
147 * the $tag parameter.
149 * The function allows for additional arguments to be added and passed to hooks.
151 * // Our filter callback function
152 * function example_callback( $string, $arg1, $arg2 ) {
153 * // (maybe) modify $string
156 * add_filter( 'example_filter', 'example_callback', 10, 3 );
159 * * Apply the filters by calling the 'example_callback' function we
160 * * "hooked" to 'example_filter' using the add_filter() function above.
161 * * - 'example_filter' is the filter hook $tag
162 * * - 'filter me' is the value being filtered
163 * * - $arg1 and $arg2 are the additional arguments passed to the callback.
164 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
168 * @global array $wp_filter Stores all of the filters.
169 * @global array $merged_filters Merges the filter hooks using this function.
170 * @global array $wp_current_filter Stores the list of current filters with the current one last.
172 * @param string $tag The name of the filter hook.
173 * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
174 * @param mixed $var Additional variables passed to the functions hooked to `$tag`.
175 * @return mixed The filtered value after all hooked functions are applied to it.
177 function apply_filters( $tag, $value ) {
178 global $wp_filter, $merged_filters, $wp_current_filter;
182 // Do 'all' actions first.
183 if ( isset($wp_filter['all']) ) {
184 $wp_current_filter[] = $tag;
185 $args = func_get_args();
186 _wp_call_all_hook($args);
189 if ( !isset($wp_filter[$tag]) ) {
190 if ( isset($wp_filter['all']) )
191 array_pop($wp_current_filter);
195 if ( !isset($wp_filter['all']) )
196 $wp_current_filter[] = $tag;
199 if ( !isset( $merged_filters[ $tag ] ) ) {
200 ksort($wp_filter[$tag]);
201 $merged_filters[ $tag ] = true;
204 reset( $wp_filter[ $tag ] );
207 $args = func_get_args();
210 foreach( (array) current($wp_filter[$tag]) as $the_ )
211 if ( !is_null($the_['function']) ){
213 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
216 } while ( next($wp_filter[$tag]) !== false );
218 array_pop( $wp_current_filter );
224 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
228 * @see apply_filters() This function is identical, but the arguments passed to the
229 * functions hooked to `$tag` are supplied using an array.
231 * @global array $wp_filter Stores all of the filters
232 * @global array $merged_filters Merges the filter hooks using this function.
233 * @global array $wp_current_filter Stores the list of current filters with the current one last
235 * @param string $tag The name of the filter hook.
236 * @param array $args The arguments supplied to the functions hooked to $tag.
237 * @return mixed The filtered value after all hooked functions are applied to it.
239 function apply_filters_ref_array($tag, $args) {
240 global $wp_filter, $merged_filters, $wp_current_filter;
242 // Do 'all' actions first
243 if ( isset($wp_filter['all']) ) {
244 $wp_current_filter[] = $tag;
245 $all_args = func_get_args();
246 _wp_call_all_hook($all_args);
249 if ( !isset($wp_filter[$tag]) ) {
250 if ( isset($wp_filter['all']) )
251 array_pop($wp_current_filter);
255 if ( !isset($wp_filter['all']) )
256 $wp_current_filter[] = $tag;
259 if ( !isset( $merged_filters[ $tag ] ) ) {
260 ksort($wp_filter[$tag]);
261 $merged_filters[ $tag ] = true;
264 reset( $wp_filter[ $tag ] );
267 foreach( (array) current($wp_filter[$tag]) as $the_ )
268 if ( !is_null($the_['function']) )
269 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
271 } while ( next($wp_filter[$tag]) !== false );
273 array_pop( $wp_current_filter );
279 * Removes a function from a specified filter hook.
281 * This function removes a function attached to a specified filter hook. This
282 * method can be used to remove default functions attached to a specific filter
283 * hook and possibly replace them with a substitute.
285 * To remove a hook, the $function_to_remove and $priority arguments must match
286 * when the hook was added. This goes for both filters and actions. No warning
287 * will be given on removal failure.
291 * @param string $tag The filter hook to which the function to be removed is hooked.
292 * @param callback $function_to_remove The name of the function which should be removed.
293 * @param int $priority Optional. The priority of the function. Default 10.
294 * @return boolean Whether the function existed before it was removed.
296 function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
297 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
299 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
302 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
303 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) {
304 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] );
306 if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {
307 $GLOBALS['wp_filter'][ $tag ] = array();
309 unset( $GLOBALS['merged_filters'][ $tag ] );
316 * Remove all of the hooks from a filter.
320 * @param string $tag The filter to remove hooks from.
321 * @param int|bool $priority Optional. The priority number to remove. Default false.
322 * @return bool True when finished.
324 function remove_all_filters( $tag, $priority = false ) {
325 global $wp_filter, $merged_filters;
327 if ( isset( $wp_filter[ $tag ]) ) {
328 if ( false === $priority ) {
329 $wp_filter[ $tag ] = array();
330 } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) {
331 $wp_filter[ $tag ][ $priority ] = array();
335 if ( isset( $merged_filters[ $tag ] ) ) {
336 unset( $merged_filters[ $tag ] );
343 * Retrieve the name of the current filter or action.
347 * @return string Hook name of the current filter or action.
349 function current_filter() {
350 global $wp_current_filter;
351 return end( $wp_current_filter );
355 * Retrieve the name of the current action.
359 * @return string Hook name of the current action.
361 function current_action() {
362 return current_filter();
366 * Retrieve the name of a filter currently being processed.
368 * The function current_filter() only returns the most recent filter or action
369 * being executed. did_action() returns true once the action is initially
372 * This function allows detection for any filter currently being
373 * executed (despite not being the most recent filter to fire, in the case of
374 * hooks called from hook callbacks) to be verified.
378 * @see current_filter()
380 * @global array $wp_current_filter Current filter.
382 * @param null|string $filter Optional. Filter to check. Defaults to null, which
383 * checks if any filter is currently being run.
384 * @return bool Whether the filter is currently in the stack.
386 function doing_filter( $filter = null ) {
387 global $wp_current_filter;
389 if ( null === $filter ) {
390 return ! empty( $wp_current_filter );
393 return in_array( $filter, $wp_current_filter );
397 * Retrieve the name of an action currently being processed.
401 * @param string|null $action Optional. Action to check. Defaults to null, which checks
402 * if any action is currently being run.
403 * @return bool Whether the action is currently in the stack.
405 function doing_action( $action = null ) {
406 return doing_filter( $action );
410 * Hooks a function on to a specific action.
412 * Actions are the hooks that the WordPress core launches at specific points
413 * during execution, or when specific events occur. Plugins can specify that
414 * one or more of its PHP functions are executed at these points, using the
419 * @param string $tag The name of the action to which the $function_to_add is hooked.
420 * @param callback $function_to_add The name of the function you wish to be called.
421 * @param int $priority Optional. Used to specify the order in which the functions
422 * associated with a particular action are executed. Default 10.
423 * Lower numbers correspond with earlier execution,
424 * and functions with the same priority are executed
425 * in the order in which they were added to the action.
426 * @param int $accepted_args Optional. The number of arguments the function accept. Default 1.
427 * @return bool Will always return true.
429 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
430 return add_filter($tag, $function_to_add, $priority, $accepted_args);
434 * Execute functions hooked on a specific action hook.
436 * This function invokes all functions attached to action hook `$tag`. It is
437 * possible to create new action hooks by simply calling this function,
438 * specifying the name of the new hook using the `$tag` parameter.
440 * You can pass extra arguments to the hooks, much like you can with
441 * {@see apply_filters()}.
445 * @global array $wp_filter Stores all of the filters
446 * @global array $wp_actions Increments the amount of times action was triggered.
448 * @param string $tag The name of the action to be executed.
449 * @param mixed $arg Optional. Additional arguments which are passed on to the
450 * functions hooked to the action. Default empty.
451 * @return null Will return null if $tag does not exist in $wp_filter array.
453 function do_action($tag, $arg = '') {
454 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
456 if ( ! isset($wp_actions[$tag]) )
457 $wp_actions[$tag] = 1;
461 // Do 'all' actions first
462 if ( isset($wp_filter['all']) ) {
463 $wp_current_filter[] = $tag;
464 $all_args = func_get_args();
465 _wp_call_all_hook($all_args);
468 if ( !isset($wp_filter[$tag]) ) {
469 if ( isset($wp_filter['all']) )
470 array_pop($wp_current_filter);
474 if ( !isset($wp_filter['all']) )
475 $wp_current_filter[] = $tag;
478 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
482 for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
483 $args[] = func_get_arg($a);
486 if ( !isset( $merged_filters[ $tag ] ) ) {
487 ksort($wp_filter[$tag]);
488 $merged_filters[ $tag ] = true;
491 reset( $wp_filter[ $tag ] );
494 foreach ( (array) current($wp_filter[$tag]) as $the_ )
495 if ( !is_null($the_['function']) )
496 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
498 } while ( next($wp_filter[$tag]) !== false );
500 array_pop($wp_current_filter);
504 * Retrieve the number of times an action is fired.
508 * @global array $wp_actions Increments the amount of times action was triggered.
510 * @param string $tag The name of the action hook.
511 * @return int The number of times action hook $tag is fired.
513 function did_action($tag) {
516 if ( ! isset( $wp_actions[ $tag ] ) )
519 return $wp_actions[$tag];
523 * Execute functions hooked on a specific action hook, specifying arguments in an array.
527 * @see do_action() This function is identical, but the arguments passed to the
528 * functions hooked to $tag< are supplied using an array.
529 * @global array $wp_filter Stores all of the filters
530 * @global array $wp_actions Increments the amount of times action was triggered.
532 * @param string $tag The name of the action to be executed.
533 * @param array $args The arguments supplied to the functions hooked to `$tag`.
534 * @return null Will return null if `$tag` does not exist in `$wp_filter` array.
536 function do_action_ref_array($tag, $args) {
537 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
539 if ( ! isset($wp_actions[$tag]) )
540 $wp_actions[$tag] = 1;
544 // Do 'all' actions first
545 if ( isset($wp_filter['all']) ) {
546 $wp_current_filter[] = $tag;
547 $all_args = func_get_args();
548 _wp_call_all_hook($all_args);
551 if ( !isset($wp_filter[$tag]) ) {
552 if ( isset($wp_filter['all']) )
553 array_pop($wp_current_filter);
557 if ( !isset($wp_filter['all']) )
558 $wp_current_filter[] = $tag;
561 if ( !isset( $merged_filters[ $tag ] ) ) {
562 ksort($wp_filter[$tag]);
563 $merged_filters[ $tag ] = true;
566 reset( $wp_filter[ $tag ] );
569 foreach( (array) current($wp_filter[$tag]) as $the_ )
570 if ( !is_null($the_['function']) )
571 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
573 } while ( next($wp_filter[$tag]) !== false );
575 array_pop($wp_current_filter);
579 * Check if any action has been registered for a hook.
583 * @see has_filter() has_action() is an alias of has_filter().
585 * @param string $tag The name of the action hook.
586 * @param callback|bool $function_to_check Optional. The callback to check for. Default false.
587 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has
588 * anything registered. When checking a specific function, the priority of that
589 * hook is returned, or false if the function is not attached. When using the
590 * $function_to_check argument, this function may return a non-boolean value
591 * that evaluates to false (e.g.) 0, so use the === operator for testing the
594 function has_action($tag, $function_to_check = false) {
595 return has_filter($tag, $function_to_check);
599 * Removes a function from a specified action hook.
601 * This function removes a function attached to a specified action hook. This
602 * method can be used to remove default functions attached to a specific filter
603 * hook and possibly replace them with a substitute.
607 * @param string $tag The action hook to which the function to be removed is hooked.
608 * @param callback $function_to_remove The name of the function which should be removed.
609 * @param int $priority Optional. The priority of the function. Default 10.
610 * @return boolean Whether the function is removed.
612 function remove_action( $tag, $function_to_remove, $priority = 10 ) {
613 return remove_filter( $tag, $function_to_remove, $priority );
617 * Remove all of the hooks from an action.
621 * @param string $tag The action to remove hooks from.
622 * @param int|bool $priority The priority number to remove them from. Default false.
623 * @return bool True when finished.
625 function remove_all_actions($tag, $priority = false) {
626 return remove_all_filters($tag, $priority);
630 // Functions for handling plugins.
634 * Gets the basename of a plugin.
636 * This method extracts the name of a plugin from its filename.
640 * @param string $file The filename of plugin.
641 * @return string The name of a plugin.
643 function plugin_basename( $file ) {
644 global $wp_plugin_paths;
646 foreach ( $wp_plugin_paths as $dir => $realdir ) {
647 if ( strpos( $file, $realdir ) === 0 ) {
648 $file = $dir . substr( $file, strlen( $realdir ) );
652 $file = wp_normalize_path( $file );
653 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
654 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
656 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
657 $file = trim($file, '/');
662 * Register a plugin's real path.
664 * This is used in plugin_basename() to resolve symlinked paths.
668 * @see plugin_basename()
670 * @param string $file Known path to the file.
671 * @return bool Whether the path was able to be registered.
673 function wp_register_plugin_realpath( $file ) {
674 global $wp_plugin_paths;
676 // Normalize, but store as static to avoid recalculation of a constant value
677 static $wp_plugin_path, $wpmu_plugin_path;
678 if ( ! isset( $wp_plugin_path ) ) {
679 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
680 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
683 $plugin_path = wp_normalize_path( dirname( $file ) );
684 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
686 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
690 if ( $plugin_path !== $plugin_realpath ) {
691 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
698 * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
702 * @param string $file The filename of the plugin (__FILE__).
703 * @return string the filesystem path of the directory that contains the plugin.
705 function plugin_dir_path( $file ) {
706 return trailingslashit( dirname( $file ) );
710 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
714 * @param string $file The filename of the plugin (__FILE__).
715 * @return string the URL path of the directory that contains the plugin.
717 function plugin_dir_url( $file ) {
718 return trailingslashit( plugins_url( '', $file ) );
722 * Set the activation hook for a plugin.
724 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
725 * called. In the name of this hook, PLUGINNAME is replaced with the name
726 * of the plugin, including the optional subdirectory. For example, when the
727 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
728 * the name of this hook will become 'activate_sampleplugin/sample.php'.
730 * When the plugin consists of only one file and is (as by default) located at
731 * wp-content/plugins/sample.php the name of this hook will be
732 * 'activate_sample.php'.
736 * @param string $file The filename of the plugin including the path.
737 * @param callback $function The function hooked to the 'activate_PLUGIN' action.
739 function register_activation_hook($file, $function) {
740 $file = plugin_basename($file);
741 add_action('activate_' . $file, $function);
745 * Set the deactivation hook for a plugin.
747 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
748 * called. In the name of this hook, PLUGINNAME is replaced with the name
749 * of the plugin, including the optional subdirectory. For example, when the
750 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
751 * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
753 * When the plugin consists of only one file and is (as by default) located at
754 * wp-content/plugins/sample.php the name of this hook will be
755 * 'deactivate_sample.php'.
759 * @param string $file The filename of the plugin including the path.
760 * @param callback $function The function hooked to the 'deactivate_PLUGIN' action.
762 function register_deactivation_hook($file, $function) {
763 $file = plugin_basename($file);
764 add_action('deactivate_' . $file, $function);
768 * Set the uninstallation hook for a plugin.
770 * Registers the uninstall hook that will be called when the user clicks on the
771 * uninstall link that calls for the plugin to uninstall itself. The link won't
772 * be active unless the plugin hooks into the action.
774 * The plugin should not run arbitrary code outside of functions, when
775 * registering the uninstall hook. In order to run using the hook, the plugin
776 * will have to be included, which means that any code laying outside of a
777 * function will be run during the uninstall process. The plugin should not
778 * hinder the uninstall process.
780 * If the plugin can not be written without running code within the plugin, then
781 * the plugin should create a file named 'uninstall.php' in the base plugin
782 * folder. This file will be called, if it exists, during the uninstall process
783 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
784 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
789 * @param string $file Plugin file.
790 * @param callback $callback The callback to run when the hook is called. Must be
791 * a static method or function.
793 function register_uninstall_hook( $file, $callback ) {
794 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
795 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
800 * The option should not be autoloaded, because it is not needed in most
801 * cases. Emphasis should be put on using the 'uninstall.php' way of
802 * uninstalling the plugin.
804 $uninstallable_plugins = (array) get_option('uninstall_plugins');
805 $uninstallable_plugins[plugin_basename($file)] = $callback;
807 update_option('uninstall_plugins', $uninstallable_plugins);
811 * Call the 'all' hook, which will process the functions hooked into it.
813 * The 'all' hook passes all of the arguments or parameters that were used for
814 * the hook, which this function was called for.
816 * This function is used internally for apply_filters(), do_action(), and
817 * do_action_ref_array() and is not meant to be used from outside those
818 * functions. This function does not check for the existence of the all hook, so
819 * it will fail unless the all hook exists prior to this function call.
824 * @param array $args The collected parameters from the hook that was called.
826 function _wp_call_all_hook($args) {
829 reset( $wp_filter['all'] );
831 foreach( (array) current($wp_filter['all']) as $the_ )
832 if ( !is_null($the_['function']) )
833 call_user_func_array($the_['function'], $args);
835 } while ( next($wp_filter['all']) !== false );
839 * Build Unique ID for storage and retrieval.
841 * The old way to serialize the callback caused issues and this function is the
842 * solution. It works by checking for objects and creating a new property in
843 * the class to keep track of the object and new objects of the same class that
846 * It also allows for the removal of actions and filters for objects after they
847 * change class properties. It is possible to include the property $wp_filter_id
848 * in your class and set it to "null" or a number to bypass the workaround.
849 * However this will prevent you from adding new classes and any new classes
850 * will overwrite the previous hook by the same class.
852 * Functions and static method callbacks are just returned as strings and
853 * shouldn't have any speed penalty.
855 * @link https://core.trac.wordpress.org/ticket/3875
860 * @global array $wp_filter Storage for all of the filters and actions.
862 * @param string $tag Used in counting how many hooks were applied
863 * @param callback $function Used for creating unique id
864 * @param int|bool $priority Used in counting how many hooks were applied. If === false
865 * and $function is an object reference, we return the unique
866 * id only if it already has one, false otherwise.
867 * @return string|bool Unique ID for usage as array key or false if $priority === false
868 * and $function is an object reference, and it does not already have
871 function _wp_filter_build_unique_id($tag, $function, $priority) {
873 static $filter_id_count = 0;
875 if ( is_string($function) )
878 if ( is_object($function) ) {
879 // Closures are currently implemented as objects
880 $function = array( $function, '' );
882 $function = (array) $function;
885 if (is_object($function[0]) ) {
886 // Object Class Calling
887 if ( function_exists('spl_object_hash') ) {
888 return spl_object_hash($function[0]) . $function[1];
890 $obj_idx = get_class($function[0]).$function[1];
891 if ( !isset($function[0]->wp_filter_id) ) {
892 if ( false === $priority )
894 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
895 $function[0]->wp_filter_id = $filter_id_count;
898 $obj_idx .= $function[0]->wp_filter_id;
903 } elseif ( is_string( $function[0] ) ) {
905 return $function[0] . '::' . $function[1];