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 http://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 * Hooks 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.
48 * 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' );
58 * Since WordPress 1.5.1, bound callbacks can take as many arguments as are
59 * passed as parameters in the corresponding apply_filters() call. The $accepted_args
60 * parameter allows for calling functions only when the number of args match.
62 * <strong>Note:</strong> the function will return true whether or not the callback
63 * is valid. It is up to you to take care. This is done for optimization purposes,
64 * so everything is as quick as possible.
66 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
67 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, 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) The order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
75 * @param int $accepted_args (optional) The number of arguments the function accepts.
77 * @return boolean true
79 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
80 global $wp_filter, $merged_filters;
82 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
83 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
84 unset( $merged_filters[ $tag ] );
89 * Check if any filter has been registered for a hook.
93 * @global array $wp_filter Stores all of the filters
95 * @param string $tag The name of the filter hook.
96 * @param callback $function_to_check optional.
97 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
98 * When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
99 * When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
100 * (e.g.) 0, so use the === operator for testing the return value.
102 function has_filter($tag, $function_to_check = false) {
105 $has = !empty($wp_filter[$tag]);
106 if ( false === $function_to_check || false == $has )
109 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
112 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
113 if ( isset($wp_filter[$tag][$priority][$idx]) )
121 * Call the functions added to a filter hook.
123 * The callback functions attached to filter hook $tag are invoked by calling
124 * this function. This function can be used to create a new filter hook by
125 * simply calling this function with the name of the new hook specified using
126 * the $tag parameter.
128 * The function allows for additional arguments to be added and passed to hooks.
130 * // Our filter callback function
131 * function example_callback( $string, $arg1, $arg2 ) {
132 * // (maybe) modify $string
135 * add_filter( 'example_filter', 'example_callback', 10, 3 );
137 * // Apply the filters by calling the 'example_callback' function we
138 * // "hooked" to 'example_filter' using the add_filter() function above.
139 * // - 'example_filter' is the filter hook $tag
140 * // - 'filter me' is the value being filtered
141 * // - $arg1 and $arg2 are the additional arguments passed to the callback.
142 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
145 * @global array $wp_filter Stores all of the filters
146 * @global array $merged_filters Merges the filter hooks using this function.
147 * @global array $wp_current_filter stores the list of current filters with the current one last
151 * @param string $tag The name of the filter hook.
152 * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
153 * @param mixed $var Additional variables passed to the functions hooked to <tt>$tag</tt>.
154 * @return mixed The filtered value after all hooked functions are applied to it.
156 function apply_filters( $tag, $value ) {
157 global $wp_filter, $merged_filters, $wp_current_filter;
161 // Do 'all' actions first
162 if ( isset($wp_filter['all']) ) {
163 $wp_current_filter[] = $tag;
164 $args = func_get_args();
165 _wp_call_all_hook($args);
168 if ( !isset($wp_filter[$tag]) ) {
169 if ( isset($wp_filter['all']) )
170 array_pop($wp_current_filter);
174 if ( !isset($wp_filter['all']) )
175 $wp_current_filter[] = $tag;
178 if ( !isset( $merged_filters[ $tag ] ) ) {
179 ksort($wp_filter[$tag]);
180 $merged_filters[ $tag ] = true;
183 reset( $wp_filter[ $tag ] );
186 $args = func_get_args();
189 foreach( (array) current($wp_filter[$tag]) as $the_ )
190 if ( !is_null($the_['function']) ){
192 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
195 } while ( next($wp_filter[$tag]) !== false );
197 array_pop( $wp_current_filter );
203 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
205 * @see apply_filters() This function is identical, but the arguments passed to the
206 * functions hooked to <tt>$tag</tt> are supplied using an array.
209 * @global array $wp_filter Stores all of the filters
210 * @global array $merged_filters Merges the filter hooks using this function.
211 * @global array $wp_current_filter stores the list of current filters with the current one last
213 * @param string $tag The name of the filter hook.
214 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
215 * @return mixed The filtered value after all hooked functions are applied to it.
217 function apply_filters_ref_array($tag, $args) {
218 global $wp_filter, $merged_filters, $wp_current_filter;
220 // Do 'all' actions first
221 if ( isset($wp_filter['all']) ) {
222 $wp_current_filter[] = $tag;
223 $all_args = func_get_args();
224 _wp_call_all_hook($all_args);
227 if ( !isset($wp_filter[$tag]) ) {
228 if ( isset($wp_filter['all']) )
229 array_pop($wp_current_filter);
233 if ( !isset($wp_filter['all']) )
234 $wp_current_filter[] = $tag;
237 if ( !isset( $merged_filters[ $tag ] ) ) {
238 ksort($wp_filter[$tag]);
239 $merged_filters[ $tag ] = true;
242 reset( $wp_filter[ $tag ] );
245 foreach( (array) current($wp_filter[$tag]) as $the_ )
246 if ( !is_null($the_['function']) )
247 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
249 } while ( next($wp_filter[$tag]) !== false );
251 array_pop( $wp_current_filter );
257 * Removes a function from a specified filter hook.
259 * This function removes a function attached to a specified filter hook. This
260 * method can be used to remove default functions attached to a specific filter
261 * hook and possibly replace them with a substitute.
263 * To remove a hook, the $function_to_remove and $priority arguments must match
264 * when the hook was added. This goes for both filters and actions. No warning
265 * will be given on removal failure.
269 * @param string $tag The filter hook to which the function to be removed is hooked.
270 * @param callback $function_to_remove The name of the function which should be removed.
271 * @param int $priority optional. The priority of the function (default: 10).
272 * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
273 * @return boolean Whether the function existed before it was removed.
275 function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
276 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
278 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
281 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
282 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
283 unset($GLOBALS['wp_filter'][$tag][$priority]);
284 unset($GLOBALS['merged_filters'][$tag]);
291 * Remove all of the hooks from a filter.
295 * @param string $tag The filter to remove hooks from.
296 * @param int $priority The priority number to remove.
297 * @return bool True when finished.
299 function remove_all_filters($tag, $priority = false) {
300 global $wp_filter, $merged_filters;
302 if( isset($wp_filter[$tag]) ) {
303 if( false !== $priority && isset($wp_filter[$tag][$priority]) )
304 unset($wp_filter[$tag][$priority]);
306 unset($wp_filter[$tag]);
309 if( isset($merged_filters[$tag]) )
310 unset($merged_filters[$tag]);
316 * Retrieve the name of the current filter or action.
320 * @return string Hook name of the current filter or action.
322 function current_filter() {
323 global $wp_current_filter;
324 return end( $wp_current_filter );
328 * Retrieve the name of the current action.
332 * @uses current_filter()
334 * @return string Hook name of the current action.
336 function current_action() {
337 return current_filter();
341 * Retrieve the name of a filter currently being processed.
343 * The function current_filter() only returns the most recent filter or action
344 * being executed. did_action() returns true once the action is initially
345 * processed. This function allows detection for any filter currently being
346 * executed (despite not being the most recent filter to fire, in the case of
347 * hooks called from hook callbacks) to be verified.
351 * @see current_filter()
353 * @global array $wp_current_filter Current filter.
355 * @param null|string $filter Optional. Filter to check. Defaults to null, which
356 * checks if any filter is currently being run.
357 * @return bool Whether the filter is currently in the stack
359 function doing_filter( $filter = null ) {
360 global $wp_current_filter;
362 if ( null === $filter ) {
363 return ! empty( $wp_current_filter );
366 return in_array( $filter, $wp_current_filter );
370 * Retrieve the name of an action currently being processed.
374 * @uses doing_filter()
376 * @param string|null $action Optional. Action to check. Defaults to null, which checks
377 * if any action is currently being run.
378 * @return bool Whether the action is currently in the stack.
380 function doing_action( $action = null ) {
381 return doing_filter( $action );
385 * Hooks a function on to a specific action.
387 * Actions are the hooks that the WordPress core launches at specific points
388 * during execution, or when specific events occur. Plugins can specify that
389 * one or more of its PHP functions are executed at these points, using the
392 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
396 * @param string $tag The name of the action to which the $function_to_add is hooked.
397 * @param callback $function_to_add The name of the function you wish to be called.
398 * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
399 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
401 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
402 return add_filter($tag, $function_to_add, $priority, $accepted_args);
406 * Execute functions hooked on a specific action hook.
408 * This function invokes all functions attached to action hook $tag. It is
409 * possible to create new action hooks by simply calling this function,
410 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
412 * You can pass extra arguments to the hooks, much like you can with
415 * @see apply_filters() This function works similar with the exception that
416 * nothing is returned and only the functions or methods are called.
420 * @global array $wp_filter Stores all of the filters
421 * @global array $wp_actions Increments the amount of times action was triggered.
423 * @param string $tag The name of the action to be executed.
424 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
425 * @return null Will return null if $tag does not exist in $wp_filter array
427 function do_action($tag, $arg = '') {
428 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
430 if ( ! isset($wp_actions[$tag]) )
431 $wp_actions[$tag] = 1;
435 // Do 'all' actions first
436 if ( isset($wp_filter['all']) ) {
437 $wp_current_filter[] = $tag;
438 $all_args = func_get_args();
439 _wp_call_all_hook($all_args);
442 if ( !isset($wp_filter[$tag]) ) {
443 if ( isset($wp_filter['all']) )
444 array_pop($wp_current_filter);
448 if ( !isset($wp_filter['all']) )
449 $wp_current_filter[] = $tag;
452 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
456 for ( $a = 2; $a < func_num_args(); $a++ )
457 $args[] = func_get_arg($a);
460 if ( !isset( $merged_filters[ $tag ] ) ) {
461 ksort($wp_filter[$tag]);
462 $merged_filters[ $tag ] = true;
465 reset( $wp_filter[ $tag ] );
468 foreach ( (array) current($wp_filter[$tag]) as $the_ )
469 if ( !is_null($the_['function']) )
470 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
472 } while ( next($wp_filter[$tag]) !== false );
474 array_pop($wp_current_filter);
478 * Retrieve the number of times an action is fired.
482 * @global array $wp_actions Increments the amount of times action was triggered.
484 * @param string $tag The name of the action hook.
485 * @return int The number of times action hook <tt>$tag</tt> is fired
487 function did_action($tag) {
490 if ( ! isset( $wp_actions[ $tag ] ) )
493 return $wp_actions[$tag];
497 * Execute functions hooked on a specific action hook, specifying arguments in an array.
499 * @see do_action() This function is identical, but the arguments passed to the
500 * functions hooked to <tt>$tag</tt> are supplied using an array.
504 * @global array $wp_filter Stores all of the filters
505 * @global array $wp_actions Increments the amount of times action was triggered.
507 * @param string $tag The name of the action to be executed.
508 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
509 * @return null Will return null if $tag does not exist in $wp_filter array
511 function do_action_ref_array($tag, $args) {
512 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
514 if ( ! isset($wp_actions[$tag]) )
515 $wp_actions[$tag] = 1;
519 // Do 'all' actions first
520 if ( isset($wp_filter['all']) ) {
521 $wp_current_filter[] = $tag;
522 $all_args = func_get_args();
523 _wp_call_all_hook($all_args);
526 if ( !isset($wp_filter[$tag]) ) {
527 if ( isset($wp_filter['all']) )
528 array_pop($wp_current_filter);
532 if ( !isset($wp_filter['all']) )
533 $wp_current_filter[] = $tag;
536 if ( !isset( $merged_filters[ $tag ] ) ) {
537 ksort($wp_filter[$tag]);
538 $merged_filters[ $tag ] = true;
541 reset( $wp_filter[ $tag ] );
544 foreach( (array) current($wp_filter[$tag]) as $the_ )
545 if ( !is_null($the_['function']) )
546 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
548 } while ( next($wp_filter[$tag]) !== false );
550 array_pop($wp_current_filter);
554 * Check if any action has been registered for a hook.
558 * @see has_filter() has_action() is an alias of has_filter().
560 * @param string $tag The name of the action hook.
561 * @param callback $function_to_check optional.
562 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
563 * When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
564 * When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
565 * (e.g.) 0, so use the === operator for testing the return value.
567 function has_action($tag, $function_to_check = false) {
568 return has_filter($tag, $function_to_check);
572 * Removes a function from a specified action hook.
574 * This function removes a function attached to a specified action hook. This
575 * method can be used to remove default functions attached to a specific filter
576 * hook and possibly replace them with a substitute.
580 * @param string $tag The action hook to which the function to be removed is hooked.
581 * @param callback $function_to_remove The name of the function which should be removed.
582 * @param int $priority optional The priority of the function (default: 10).
583 * @return boolean Whether the function is removed.
585 function remove_action( $tag, $function_to_remove, $priority = 10 ) {
586 return remove_filter( $tag, $function_to_remove, $priority );
590 * Remove all of the hooks from an action.
594 * @param string $tag The action to remove hooks from.
595 * @param int $priority The priority number to remove them from.
596 * @return bool True when finished.
598 function remove_all_actions($tag, $priority = false) {
599 return remove_all_filters($tag, $priority);
603 // Functions for handling plugins.
607 * Gets the basename of a plugin.
609 * This method extracts the name of a plugin from its filename.
615 * @param string $file The filename of plugin.
616 * @return string The name of a plugin.
617 * @uses WP_PLUGIN_DIR
619 function plugin_basename( $file ) {
620 global $wp_plugin_paths;
622 foreach ( $wp_plugin_paths as $dir => $realdir ) {
623 if ( strpos( $file, $realdir ) === 0 ) {
624 $file = $dir . substr( $file, strlen( $realdir ) );
628 $file = wp_normalize_path( $file );
629 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
630 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
632 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
633 $file = trim($file, '/');
638 * Register a plugin's real path.
640 * This is used in plugin_basename() to resolve symlinked paths.
644 * @see plugin_basename()
646 * @param string $file Known path to the file.
647 * @return bool Whether the path was able to be registered.
649 function wp_register_plugin_realpath( $file ) {
650 global $wp_plugin_paths;
652 // Normalize, but store as static to avoid recalculation of a constant value
653 static $wp_plugin_path, $wpmu_plugin_path;
654 if ( ! isset( $wp_plugin_path ) ) {
655 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
656 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
659 $plugin_path = wp_normalize_path( dirname( $file ) );
660 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
662 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
666 if ( $plugin_path !== $plugin_realpath ) {
667 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
674 * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
678 * @param string $file The filename of the plugin (__FILE__)
679 * @return string the filesystem path of the directory that contains the plugin
681 function plugin_dir_path( $file ) {
682 return trailingslashit( dirname( $file ) );
686 * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
690 * @param string $file The filename of the plugin (__FILE__)
691 * @return string the URL path of the directory that contains the plugin
693 function plugin_dir_url( $file ) {
694 return trailingslashit( plugins_url( '', $file ) );
698 * Set the activation hook for a plugin.
700 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
701 * called. In the name of this hook, PLUGINNAME is replaced with the name
702 * of the plugin, including the optional subdirectory. For example, when the
703 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
704 * the name of this hook will become 'activate_sampleplugin/sample.php'.
706 * When the plugin consists of only one file and is (as by default) located at
707 * wp-content/plugins/sample.php the name of this hook will be
708 * 'activate_sample.php'.
712 * @param string $file The filename of the plugin including the path.
713 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
715 function register_activation_hook($file, $function) {
716 $file = plugin_basename($file);
717 add_action('activate_' . $file, $function);
721 * Set the deactivation hook for a plugin.
723 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
724 * called. In the name of this hook, PLUGINNAME is replaced with the name
725 * of the plugin, including the optional subdirectory. For example, when the
726 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
727 * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
729 * When the plugin consists of only one file and is (as by default) located at
730 * wp-content/plugins/sample.php the name of this hook will be
731 * 'deactivate_sample.php'.
735 * @param string $file The filename of the plugin including the path.
736 * @param callback $function the function hooked to the 'deactivate_PLUGIN' action.
738 function register_deactivation_hook($file, $function) {
739 $file = plugin_basename($file);
740 add_action('deactivate_' . $file, $function);
744 * Set the uninstallation hook for a plugin.
746 * Registers the uninstall hook that will be called when the user clicks on the
747 * uninstall link that calls for the plugin to uninstall itself. The link won't
748 * be active unless the plugin hooks into the action.
750 * The plugin should not run arbitrary code outside of functions, when
751 * registering the uninstall hook. In order to run using the hook, the plugin
752 * will have to be included, which means that any code laying outside of a
753 * function will be run during the uninstall process. The plugin should not
754 * hinder the uninstall process.
756 * If the plugin can not be written without running code within the plugin, then
757 * the plugin should create a file named 'uninstall.php' in the base plugin
758 * folder. This file will be called, if it exists, during the uninstall process
759 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
760 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
765 * @param string $file
766 * @param callback $callback The callback to run when the hook is called. Must be a static method or function.
768 function register_uninstall_hook( $file, $callback ) {
769 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
770 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
774 // The option should not be autoloaded, because it is not needed in most
775 // cases. Emphasis should be put on using the 'uninstall.php' way of
776 // uninstalling the plugin.
777 $uninstallable_plugins = (array) get_option('uninstall_plugins');
778 $uninstallable_plugins[plugin_basename($file)] = $callback;
779 update_option('uninstall_plugins', $uninstallable_plugins);
783 * Calls the 'all' hook, which will process the functions hooked into it.
785 * The 'all' hook passes all of the arguments or parameters that were used for
786 * the hook, which this function was called for.
788 * This function is used internally for apply_filters(), do_action(), and
789 * do_action_ref_array() and is not meant to be used from outside those
790 * functions. This function does not check for the existence of the all hook, so
791 * it will fail unless the all hook exists prior to this function call.
796 * @uses $wp_filter Used to process all of the functions in the 'all' hook
798 * @param array $args The collected parameters from the hook that was called.
800 function _wp_call_all_hook($args) {
803 reset( $wp_filter['all'] );
805 foreach( (array) current($wp_filter['all']) as $the_ )
806 if ( !is_null($the_['function']) )
807 call_user_func_array($the_['function'], $args);
809 } while ( next($wp_filter['all']) !== false );
813 * Build Unique ID for storage and retrieval.
815 * The old way to serialize the callback caused issues and this function is the
816 * solution. It works by checking for objects and creating an a new property in
817 * the class to keep track of the object and new objects of the same class that
820 * It also allows for the removal of actions and filters for objects after they
821 * change class properties. It is possible to include the property $wp_filter_id
822 * in your class and set it to "null" or a number to bypass the workaround.
823 * However this will prevent you from adding new classes and any new classes
824 * will overwrite the previous hook by the same class.
826 * Functions and static method callbacks are just returned as strings and
827 * shouldn't have any speed penalty.
831 * @link http://trac.wordpress.org/ticket/3875
833 * @global array $wp_filter Storage for all of the filters and actions
834 * @param string $tag Used in counting how many hooks were applied
835 * @param callback $function Used for creating unique id
836 * @param 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.
837 * @return string|bool 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.
839 function _wp_filter_build_unique_id($tag, $function, $priority) {
841 static $filter_id_count = 0;
843 if ( is_string($function) )
846 if ( is_object($function) ) {
847 // Closures are currently implemented as objects
848 $function = array( $function, '' );
850 $function = (array) $function;
853 if (is_object($function[0]) ) {
854 // Object Class Calling
855 if ( function_exists('spl_object_hash') ) {
856 return spl_object_hash($function[0]) . $function[1];
858 $obj_idx = get_class($function[0]).$function[1];
859 if ( !isset($function[0]->wp_filter_id) ) {
860 if ( false === $priority )
862 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
863 $function[0]->wp_filter_id = $filter_id_count;
866 $obj_idx .= $function[0]->wp_filter_id;
871 } else if ( is_string($function[0]) ) {
873 return $function[0] . '::' . $function[1];