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 https://secure.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.
17 * This file should have no external dependencies.
24 // Initialize the filter globals.
25 require( dirname( __FILE__ ) . '/class-wp-hook.php' );
27 /** @var WP_Hook[] $wp_filter */
28 global $wp_filter, $wp_actions, $wp_current_filter;
31 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
36 if ( ! isset( $wp_actions ) )
37 $wp_actions = array();
39 if ( ! isset( $wp_current_filter ) )
40 $wp_current_filter = array();
43 * Hook a function or method to a specific filter action.
45 * WordPress offers filter hooks to allow plugins to modify
46 * various types of internal data at runtime.
48 * A plugin can modify data by binding a callback to a filter hook. When the filter
49 * is later applied, each bound callback is run in order of priority, and given
50 * the opportunity to modify a value by returning a new value.
52 * The following example shows how a callback function is bound to a filter hook.
54 * Note that `$example` is passed to the callback, (maybe) modified, then returned:
56 * function example_callback( $example ) {
57 * // Maybe modify $example in some way.
60 * add_filter( 'example_filter', 'example_callback' );
62 * Bound callbacks can accept from none to the total number of arguments passed as parameters
63 * in the corresponding apply_filters() call.
65 * In other words, if an apply_filters() call passes four total arguments, callbacks bound to
66 * it can accept none (the same as 1) of the arguments or up to four. The important part is that
67 * the `$accepted_args` value must reflect the number of arguments the bound callback *actually*
68 * opted to accept. If no arguments were accepted by the callback that is considered to be the
69 * same as accepting 1 argument. For example:
72 * $value = apply_filters( 'hook', $value, $arg2, $arg3 );
74 * // Accepting zero/one arguments.
75 * function example_callback() {
77 * return 'some value';
79 * add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.
81 * // Accepting two arguments (three possible).
82 * function example_callback( $value, $arg2 ) {
84 * return $maybe_modified_value;
86 * add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.
88 * *Note:* The function will return true whether or not the callback is valid.
89 * It is up to you to take care. This is done for optimization purposes, so
90 * everything is as quick as possible.
94 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
96 * @param string $tag The name of the filter to hook the $function_to_add callback to.
97 * @param callable $function_to_add The callback to be run when the filter is applied.
98 * @param int $priority Optional. Used to specify the order in which the functions
99 * associated with a particular action are executed. Default 10.
100 * Lower numbers correspond with earlier execution,
101 * and functions with the same priority are executed
102 * in the order in which they were added to the action.
103 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
106 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
108 if ( ! isset( $wp_filter[ $tag ] ) ) {
109 $wp_filter[ $tag ] = new WP_Hook();
111 $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
116 * Check if any filter has been registered for a hook.
120 * @global array $wp_filter Stores all of the filters.
122 * @param string $tag The name of the filter hook.
123 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
124 * @return false|int If $function_to_check is omitted, returns boolean for whether the hook has
125 * anything registered. When checking a specific function, the priority of that
126 * hook is returned, or false if the function is not attached. When using the
127 * $function_to_check argument, this function may return a non-boolean value
128 * that evaluates to false (e.g.) 0, so use the === operator for testing the
131 function has_filter($tag, $function_to_check = false) {
134 if ( ! isset( $wp_filter[ $tag ] ) ) {
138 return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );
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 $wp_current_filter Stores the list of current filters with the current one last.
171 * @param string $tag The name of the filter hook.
172 * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
173 * @param mixed $var,... Additional variables passed to the functions hooked to `$tag`.
174 * @return mixed The filtered value after all hooked functions are applied to it.
176 function apply_filters( $tag, $value ) {
177 global $wp_filter, $wp_current_filter;
181 // Do 'all' actions first.
182 if ( isset($wp_filter['all']) ) {
183 $wp_current_filter[] = $tag;
184 $args = func_get_args();
185 _wp_call_all_hook($args);
188 if ( !isset($wp_filter[$tag]) ) {
189 if ( isset($wp_filter['all']) )
190 array_pop($wp_current_filter);
194 if ( !isset($wp_filter['all']) )
195 $wp_current_filter[] = $tag;
198 $args = func_get_args();
200 // don't pass the tag name to WP_Hook
201 array_shift( $args );
203 $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args );
205 array_pop( $wp_current_filter );
211 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
215 * @see apply_filters() This function is identical, but the arguments passed to the
216 * functions hooked to `$tag` are supplied using an array.
218 * @global array $wp_filter Stores all of the filters
219 * @global array $wp_current_filter Stores the list of current filters with the current one last
221 * @param string $tag The name of the filter hook.
222 * @param array $args The arguments supplied to the functions hooked to $tag.
223 * @return mixed The filtered value after all hooked functions are applied to it.
225 function apply_filters_ref_array($tag, $args) {
226 global $wp_filter, $wp_current_filter;
228 // Do 'all' actions first
229 if ( isset($wp_filter['all']) ) {
230 $wp_current_filter[] = $tag;
231 $all_args = func_get_args();
232 _wp_call_all_hook($all_args);
235 if ( !isset($wp_filter[$tag]) ) {
236 if ( isset($wp_filter['all']) )
237 array_pop($wp_current_filter);
241 if ( !isset($wp_filter['all']) )
242 $wp_current_filter[] = $tag;
244 $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args );
246 array_pop( $wp_current_filter );
252 * Removes a function from a specified filter hook.
254 * This function removes a function attached to a specified filter hook. This
255 * method can be used to remove default functions attached to a specific filter
256 * hook and possibly replace them with a substitute.
258 * To remove a hook, the $function_to_remove and $priority arguments must match
259 * when the hook was added. This goes for both filters and actions. No warning
260 * will be given on removal failure.
264 * @global array $wp_filter Stores all of the filters
266 * @param string $tag The filter hook to which the function to be removed is hooked.
267 * @param callable $function_to_remove The name of the function which should be removed.
268 * @param int $priority Optional. The priority of the function. Default 10.
269 * @return bool Whether the function existed before it was removed.
271 function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
275 if ( isset( $wp_filter[ $tag ] ) ) {
276 $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority );
277 if ( ! $wp_filter[ $tag ]->callbacks ) {
278 unset( $wp_filter[ $tag ] );
286 * Remove all of the hooks from a filter.
290 * @global array $wp_filter Stores all of the filters
292 * @param string $tag The filter to remove hooks from.
293 * @param int|bool $priority Optional. The priority number to remove. Default false.
294 * @return true True when finished.
296 function remove_all_filters( $tag, $priority = false ) {
299 if ( isset( $wp_filter[ $tag ]) ) {
300 $wp_filter[ $tag ]->remove_all_filters( $priority );
301 if ( ! $wp_filter[ $tag ]->has_filters() ) {
302 unset( $wp_filter[ $tag ] );
310 * Retrieve the name of the current filter or action.
314 * @global array $wp_current_filter Stores the list of current filters with the current one last
316 * @return string Hook name of the current filter or action.
318 function current_filter() {
319 global $wp_current_filter;
320 return end( $wp_current_filter );
324 * Retrieve the name of the current action.
328 * @return string Hook name of the current action.
330 function current_action() {
331 return current_filter();
335 * Retrieve the name of a filter currently being processed.
337 * The function current_filter() only returns the most recent filter or action
338 * being executed. did_action() returns true once the action is initially
341 * This function allows detection for any filter currently being
342 * executed (despite not being the most recent filter to fire, in the case of
343 * hooks called from hook callbacks) to be verified.
347 * @see current_filter()
349 * @global array $wp_current_filter Current filter.
351 * @param null|string $filter Optional. Filter to check. Defaults to null, which
352 * checks if any filter is currently being run.
353 * @return bool Whether the filter is currently in the stack.
355 function doing_filter( $filter = null ) {
356 global $wp_current_filter;
358 if ( null === $filter ) {
359 return ! empty( $wp_current_filter );
362 return in_array( $filter, $wp_current_filter );
366 * Retrieve the name of an action currently being processed.
370 * @param string|null $action Optional. Action to check. Defaults to null, which checks
371 * if any action is currently being run.
372 * @return bool Whether the action is currently in the stack.
374 function doing_action( $action = null ) {
375 return doing_filter( $action );
379 * Hooks a function on to a specific action.
381 * Actions are the hooks that the WordPress core launches at specific points
382 * during execution, or when specific events occur. Plugins can specify that
383 * one or more of its PHP functions are executed at these points, using the
388 * @param string $tag The name of the action to which the $function_to_add is hooked.
389 * @param callable $function_to_add The name of the function you wish to be called.
390 * @param int $priority Optional. Used to specify the order in which the functions
391 * associated with a particular action are executed. Default 10.
392 * Lower numbers correspond with earlier execution,
393 * and functions with the same priority are executed
394 * in the order in which they were added to the action.
395 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
396 * @return true Will always return true.
398 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
399 return add_filter($tag, $function_to_add, $priority, $accepted_args);
403 * Execute functions hooked on a specific action hook.
405 * This function invokes all functions attached to action hook `$tag`. It is
406 * possible to create new action hooks by simply calling this function,
407 * specifying the name of the new hook using the `$tag` parameter.
409 * You can pass extra arguments to the hooks, much like you can with apply_filters().
413 * @global array $wp_filter Stores all of the filters
414 * @global array $wp_actions Increments the amount of times action was triggered.
415 * @global array $wp_current_filter Stores the list of current filters with the current one last
417 * @param string $tag The name of the action to be executed.
418 * @param mixed $arg,... Optional. Additional arguments which are passed on to the
419 * functions hooked to the action. Default empty.
421 function do_action($tag, $arg = '') {
422 global $wp_filter, $wp_actions, $wp_current_filter;
424 if ( ! isset($wp_actions[$tag]) )
425 $wp_actions[$tag] = 1;
429 // Do 'all' actions first
430 if ( isset($wp_filter['all']) ) {
431 $wp_current_filter[] = $tag;
432 $all_args = func_get_args();
433 _wp_call_all_hook($all_args);
436 if ( !isset($wp_filter[$tag]) ) {
437 if ( isset($wp_filter['all']) )
438 array_pop($wp_current_filter);
442 if ( !isset($wp_filter['all']) )
443 $wp_current_filter[] = $tag;
446 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
450 for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
451 $args[] = func_get_arg($a);
453 $wp_filter[ $tag ]->do_action( $args );
455 array_pop($wp_current_filter);
459 * Retrieve the number of times an action is fired.
463 * @global array $wp_actions Increments the amount of times action was triggered.
465 * @param string $tag The name of the action hook.
466 * @return int The number of times action hook $tag is fired.
468 function did_action($tag) {
471 if ( ! isset( $wp_actions[ $tag ] ) )
474 return $wp_actions[$tag];
478 * Execute functions hooked on a specific action hook, specifying arguments in an array.
482 * @see do_action() This function is identical, but the arguments passed to the
483 * functions hooked to $tag< are supplied using an array.
484 * @global array $wp_filter Stores all of the filters
485 * @global array $wp_actions Increments the amount of times action was triggered.
486 * @global array $wp_current_filter Stores the list of current filters with the current one last
488 * @param string $tag The name of the action to be executed.
489 * @param array $args The arguments supplied to the functions hooked to `$tag`.
491 function do_action_ref_array($tag, $args) {
492 global $wp_filter, $wp_actions, $wp_current_filter;
494 if ( ! isset($wp_actions[$tag]) )
495 $wp_actions[$tag] = 1;
499 // Do 'all' actions first
500 if ( isset($wp_filter['all']) ) {
501 $wp_current_filter[] = $tag;
502 $all_args = func_get_args();
503 _wp_call_all_hook($all_args);
506 if ( !isset($wp_filter[$tag]) ) {
507 if ( isset($wp_filter['all']) )
508 array_pop($wp_current_filter);
512 if ( !isset($wp_filter['all']) )
513 $wp_current_filter[] = $tag;
515 $wp_filter[ $tag ]->do_action( $args );
517 array_pop($wp_current_filter);
521 * Check if any action has been registered for a hook.
525 * @see has_filter() has_action() is an alias of has_filter().
527 * @param string $tag The name of the action hook.
528 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
529 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has
530 * anything registered. When checking a specific function, the priority of that
531 * hook is returned, or false if the function is not attached. When using the
532 * $function_to_check argument, this function may return a non-boolean value
533 * that evaluates to false (e.g.) 0, so use the === operator for testing the
536 function has_action($tag, $function_to_check = false) {
537 return has_filter($tag, $function_to_check);
541 * Removes a function from a specified action hook.
543 * This function removes a function attached to a specified action hook. This
544 * method can be used to remove default functions attached to a specific filter
545 * hook and possibly replace them with a substitute.
549 * @param string $tag The action hook to which the function to be removed is hooked.
550 * @param callable $function_to_remove The name of the function which should be removed.
551 * @param int $priority Optional. The priority of the function. Default 10.
552 * @return bool Whether the function is removed.
554 function remove_action( $tag, $function_to_remove, $priority = 10 ) {
555 return remove_filter( $tag, $function_to_remove, $priority );
559 * Remove all of the hooks from an action.
563 * @param string $tag The action to remove hooks from.
564 * @param int|bool $priority The priority number to remove them from. Default false.
565 * @return true True when finished.
567 function remove_all_actions($tag, $priority = false) {
568 return remove_all_filters($tag, $priority);
572 * Fires functions attached to a deprecated filter hook.
574 * When a filter hook is deprecated, the apply_filters() call is replaced with
575 * apply_filters_deprecated(), which triggers a deprecation notice and then fires
576 * the original filter hook.
580 * @see _deprecated_hook()
582 * @param string $tag The name of the filter hook.
583 * @param array $args Array of additional function arguments to be passed to apply_filters().
584 * @param string $version The version of WordPress that deprecated the hook.
585 * @param string $replacement Optional. The hook that should have been used. Default false.
586 * @param string $message Optional. A message regarding the change. Default null.
588 function apply_filters_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
589 if ( ! has_filter( $tag ) ) {
593 _deprecated_hook( $tag, $version, $replacement, $message );
595 return apply_filters_ref_array( $tag, $args );
599 * Fires functions attached to a deprecated action hook.
601 * When an action hook is deprecated, the do_action() call is replaced with
602 * do_action_deprecated(), which triggers a deprecation notice and then fires
607 * @see _deprecated_hook()
609 * @param string $tag The name of the action hook.
610 * @param array $args Array of additional function arguments to be passed to do_action().
611 * @param string $version The version of WordPress that deprecated the hook.
612 * @param string $replacement Optional. The hook that should have been used.
613 * @param string $message Optional. A message regarding the change.
615 function do_action_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
616 if ( ! has_action( $tag ) ) {
620 _deprecated_hook( $tag, $version, $replacement, $message );
622 do_action_ref_array( $tag, $args );
626 // Functions for handling plugins.
630 * Gets the basename of a plugin.
632 * This method extracts the name of a plugin from its filename.
636 * @global array $wp_plugin_paths
638 * @param string $file The filename of plugin.
639 * @return string The name of a plugin.
641 function plugin_basename( $file ) {
642 global $wp_plugin_paths;
644 // $wp_plugin_paths contains normalized paths.
645 $file = wp_normalize_path( $file );
647 arsort( $wp_plugin_paths );
648 foreach ( $wp_plugin_paths as $dir => $realdir ) {
649 if ( strpos( $file, $realdir ) === 0 ) {
650 $file = $dir . substr( $file, strlen( $realdir ) );
654 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
655 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
657 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
658 $file = trim($file, '/');
663 * Register a plugin's real path.
665 * This is used in plugin_basename() to resolve symlinked paths.
669 * @see wp_normalize_path()
671 * @global array $wp_plugin_paths
673 * @staticvar string $wp_plugin_path
674 * @staticvar string $wpmu_plugin_path
676 * @param string $file Known path to the file.
677 * @return bool Whether the path was able to be registered.
679 function wp_register_plugin_realpath( $file ) {
680 global $wp_plugin_paths;
682 // Normalize, but store as static to avoid recalculation of a constant value
683 static $wp_plugin_path = null, $wpmu_plugin_path = null;
684 if ( ! isset( $wp_plugin_path ) ) {
685 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
686 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
689 $plugin_path = wp_normalize_path( dirname( $file ) );
690 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
692 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
696 if ( $plugin_path !== $plugin_realpath ) {
697 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
704 * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
708 * @param string $file The filename of the plugin (__FILE__).
709 * @return string the filesystem path of the directory that contains the plugin.
711 function plugin_dir_path( $file ) {
712 return trailingslashit( dirname( $file ) );
716 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
720 * @param string $file The filename of the plugin (__FILE__).
721 * @return string the URL path of the directory that contains the plugin.
723 function plugin_dir_url( $file ) {
724 return trailingslashit( plugins_url( '', $file ) );
728 * Set the activation hook for a plugin.
730 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
731 * called. In the name of this hook, PLUGINNAME is replaced with the name
732 * of the plugin, including the optional subdirectory. For example, when the
733 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
734 * the name of this hook will become 'activate_sampleplugin/sample.php'.
736 * When the plugin consists of only one file and is (as by default) located at
737 * wp-content/plugins/sample.php the name of this hook will be
738 * 'activate_sample.php'.
742 * @param string $file The filename of the plugin including the path.
743 * @param callable $function The function hooked to the 'activate_PLUGIN' action.
745 function register_activation_hook($file, $function) {
746 $file = plugin_basename($file);
747 add_action('activate_' . $file, $function);
751 * Set the deactivation hook for a plugin.
753 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
754 * called. In the name of this hook, PLUGINNAME is replaced with the name
755 * of the plugin, including the optional subdirectory. For example, when the
756 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
757 * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
759 * When the plugin consists of only one file and is (as by default) located at
760 * wp-content/plugins/sample.php the name of this hook will be
761 * 'deactivate_sample.php'.
765 * @param string $file The filename of the plugin including the path.
766 * @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
768 function register_deactivation_hook($file, $function) {
769 $file = plugin_basename($file);
770 add_action('deactivate_' . $file, $function);
774 * Set the uninstallation hook for a plugin.
776 * Registers the uninstall hook that will be called when the user clicks on the
777 * uninstall link that calls for the plugin to uninstall itself. The link won't
778 * be active unless the plugin hooks into the action.
780 * The plugin should not run arbitrary code outside of functions, when
781 * registering the uninstall hook. In order to run using the hook, the plugin
782 * will have to be included, which means that any code laying outside of a
783 * function will be run during the uninstall process. The plugin should not
784 * hinder the uninstall process.
786 * If the plugin can not be written without running code within the plugin, then
787 * the plugin should create a file named 'uninstall.php' in the base plugin
788 * folder. This file will be called, if it exists, during the uninstall process
789 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
790 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
795 * @param string $file Plugin file.
796 * @param callable $callback The callback to run when the hook is called. Must be
797 * a static method or function.
799 function register_uninstall_hook( $file, $callback ) {
800 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
801 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
806 * The option should not be autoloaded, because it is not needed in most
807 * cases. Emphasis should be put on using the 'uninstall.php' way of
808 * uninstalling the plugin.
810 $uninstallable_plugins = (array) get_option('uninstall_plugins');
811 $uninstallable_plugins[plugin_basename($file)] = $callback;
813 update_option('uninstall_plugins', $uninstallable_plugins);
817 * Call the 'all' hook, which will process the functions hooked into it.
819 * The 'all' hook passes all of the arguments or parameters that were used for
820 * the hook, which this function was called for.
822 * This function is used internally for apply_filters(), do_action(), and
823 * do_action_ref_array() and is not meant to be used from outside those
824 * functions. This function does not check for the existence of the all hook, so
825 * it will fail unless the all hook exists prior to this function call.
830 * @global array $wp_filter Stores all of the filters
832 * @param array $args The collected parameters from the hook that was called.
834 function _wp_call_all_hook($args) {
837 $wp_filter['all']->do_all_hook( $args );
841 * Build Unique ID for storage and retrieval.
843 * The old way to serialize the callback caused issues and this function is the
844 * solution. It works by checking for objects and creating a new property in
845 * the class to keep track of the object and new objects of the same class that
848 * It also allows for the removal of actions and filters for objects after they
849 * change class properties. It is possible to include the property $wp_filter_id
850 * in your class and set it to "null" or a number to bypass the workaround.
851 * However this will prevent you from adding new classes and any new classes
852 * will overwrite the previous hook by the same class.
854 * Functions and static method callbacks are just returned as strings and
855 * shouldn't have any speed penalty.
857 * @link https://core.trac.wordpress.org/ticket/3875
862 * @global array $wp_filter Storage for all of the filters and actions.
863 * @staticvar int $filter_id_count
865 * @param string $tag Used in counting how many hooks were applied
866 * @param callable $function Used for creating unique id
867 * @param int|bool $priority Used in counting how many hooks were applied. If === false
868 * and $function is an object reference, we return the unique
869 * id only if it already has one, false otherwise.
870 * @return string|false Unique ID for usage as array key or false if $priority === false
871 * and $function is an object reference, and it does not already have
874 function _wp_filter_build_unique_id($tag, $function, $priority) {
876 static $filter_id_count = 0;
878 if ( is_string($function) )
881 if ( is_object($function) ) {
882 // Closures are currently implemented as objects
883 $function = array( $function, '' );
885 $function = (array) $function;
888 if (is_object($function[0]) ) {
889 // Object Class Calling
890 if ( function_exists('spl_object_hash') ) {
891 return spl_object_hash($function[0]) . $function[1];
893 $obj_idx = get_class($function[0]).$function[1];
894 if ( !isset($function[0]->wp_filter_id) ) {
895 if ( false === $priority )
897 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
898 $function[0]->wp_filter_id = $filter_id_count;
901 $obj_idx .= $function[0]->wp_filter_id;
906 } elseif ( is_string( $function[0] ) ) {
908 return $function[0] . '::' . $function[1];