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.
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 * Bound callbacks can accept from none to the total number of arguments passed as parameters
58 * in the corresponding apply_filters() call.
60 * In other words, if an apply_filters() call passes four total arguments, callbacks bound to
61 * it can accept none (the same as 1) of the arguments or up to four. The important part is that
62 * the `$accepted_args` value must reflect the number of arguments the bound callback *actually*
63 * opted to accept. If no arguments were accepted by the callback that is considered to be the
64 * same as accepting 1 argument. For example:
67 * $value = apply_filters( 'hook', $value, $arg2, $arg3 );
69 * // Accepting zero/one arguments.
70 * function example_callback() {
72 * return 'some value';
74 * add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.
76 * // Accepting two arguments (three possible).
77 * function example_callback( $value, $arg2 ) {
79 * return $maybe_modified_value;
81 * add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.
83 * *Note:* The function will return true whether or not the callback is valid.
84 * It is up to you to take care. This is done for optimization purposes, so
85 * everything is as quick as possible.
89 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
90 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added,
91 * it doesn't need to run through that process.
93 * @param string $tag The name of the filter to hook the $function_to_add callback to.
94 * @param callable $function_to_add The callback to be run when the filter is applied.
95 * @param int $priority Optional. Used to specify the order in which the functions
96 * associated with a particular action are executed. Default 10.
97 * Lower numbers correspond with earlier execution,
98 * and functions with the same priority are executed
99 * in the order in which they were added to the action.
100 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
103 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
104 global $wp_filter, $merged_filters;
106 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
107 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
108 unset( $merged_filters[ $tag ] );
113 * Check if any filter has been registered for a hook.
117 * @global array $wp_filter Stores all of the filters.
119 * @param string $tag The name of the filter hook.
120 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
121 * @return false|int If $function_to_check is omitted, returns boolean for whether the hook has
122 * anything registered. When checking a specific function, the priority of that
123 * hook is returned, or false if the function is not attached. When using the
124 * $function_to_check argument, this function may return a non-boolean value
125 * that evaluates to false (e.g.) 0, so use the === operator for testing the
128 function has_filter($tag, $function_to_check = false) {
129 // Don't reset the internal array pointer
130 $wp_filter = $GLOBALS['wp_filter'];
132 $has = ! empty( $wp_filter[ $tag ] );
134 // Make sure at least one priority has a filter callback
137 foreach ( $wp_filter[ $tag ] as $callbacks ) {
138 if ( ! empty( $callbacks ) ) {
149 if ( false === $function_to_check || false === $has )
152 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
155 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
156 if ( isset($wp_filter[$tag][$priority][$idx]) )
164 * Call the functions added to a filter hook.
166 * The callback functions attached to filter hook $tag are invoked by calling
167 * this function. This function can be used to create a new filter hook by
168 * simply calling this function with the name of the new hook specified using
169 * the $tag parameter.
171 * The function allows for additional arguments to be added and passed to hooks.
173 * // Our filter callback function
174 * function example_callback( $string, $arg1, $arg2 ) {
175 * // (maybe) modify $string
178 * add_filter( 'example_filter', 'example_callback', 10, 3 );
181 * * Apply the filters by calling the 'example_callback' function we
182 * * "hooked" to 'example_filter' using the add_filter() function above.
183 * * - 'example_filter' is the filter hook $tag
184 * * - 'filter me' is the value being filtered
185 * * - $arg1 and $arg2 are the additional arguments passed to the callback.
186 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
190 * @global array $wp_filter Stores all of the filters.
191 * @global array $merged_filters Merges the filter hooks using this function.
192 * @global array $wp_current_filter Stores the list of current filters with the current one last.
194 * @param string $tag The name of the filter hook.
195 * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
196 * @param mixed $var,... Additional variables passed to the functions hooked to `$tag`.
197 * @return mixed The filtered value after all hooked functions are applied to it.
199 function apply_filters( $tag, $value ) {
200 global $wp_filter, $merged_filters, $wp_current_filter;
204 // Do 'all' actions first.
205 if ( isset($wp_filter['all']) ) {
206 $wp_current_filter[] = $tag;
207 $args = func_get_args();
208 _wp_call_all_hook($args);
211 if ( !isset($wp_filter[$tag]) ) {
212 if ( isset($wp_filter['all']) )
213 array_pop($wp_current_filter);
217 if ( !isset($wp_filter['all']) )
218 $wp_current_filter[] = $tag;
221 if ( !isset( $merged_filters[ $tag ] ) ) {
222 ksort($wp_filter[$tag]);
223 $merged_filters[ $tag ] = true;
226 reset( $wp_filter[ $tag ] );
229 $args = func_get_args();
232 foreach ( (array) current($wp_filter[$tag]) as $the_ )
233 if ( !is_null($the_['function']) ){
235 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
238 } while ( next($wp_filter[$tag]) !== false );
240 array_pop( $wp_current_filter );
246 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
250 * @see apply_filters() This function is identical, but the arguments passed to the
251 * functions hooked to `$tag` are supplied using an array.
253 * @global array $wp_filter Stores all of the filters
254 * @global array $merged_filters Merges the filter hooks using this function.
255 * @global array $wp_current_filter Stores the list of current filters with the current one last
257 * @param string $tag The name of the filter hook.
258 * @param array $args The arguments supplied to the functions hooked to $tag.
259 * @return mixed The filtered value after all hooked functions are applied to it.
261 function apply_filters_ref_array($tag, $args) {
262 global $wp_filter, $merged_filters, $wp_current_filter;
264 // Do 'all' actions first
265 if ( isset($wp_filter['all']) ) {
266 $wp_current_filter[] = $tag;
267 $all_args = func_get_args();
268 _wp_call_all_hook($all_args);
271 if ( !isset($wp_filter[$tag]) ) {
272 if ( isset($wp_filter['all']) )
273 array_pop($wp_current_filter);
277 if ( !isset($wp_filter['all']) )
278 $wp_current_filter[] = $tag;
281 if ( !isset( $merged_filters[ $tag ] ) ) {
282 ksort($wp_filter[$tag]);
283 $merged_filters[ $tag ] = true;
286 reset( $wp_filter[ $tag ] );
289 foreach ( (array) current($wp_filter[$tag]) as $the_ )
290 if ( !is_null($the_['function']) )
291 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
293 } while ( next($wp_filter[$tag]) !== false );
295 array_pop( $wp_current_filter );
301 * Removes a function from a specified filter hook.
303 * This function removes a function attached to a specified filter hook. This
304 * method can be used to remove default functions attached to a specific filter
305 * hook and possibly replace them with a substitute.
307 * To remove a hook, the $function_to_remove and $priority arguments must match
308 * when the hook was added. This goes for both filters and actions. No warning
309 * will be given on removal failure.
313 * @global array $wp_filter Stores all of the filters
314 * @global array $merged_filters Merges the filter hooks using this function.
316 * @param string $tag The filter hook to which the function to be removed is hooked.
317 * @param callable $function_to_remove The name of the function which should be removed.
318 * @param int $priority Optional. The priority of the function. Default 10.
319 * @return bool Whether the function existed before it was removed.
321 function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
322 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
324 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
327 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
328 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) {
329 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] );
331 if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {
332 $GLOBALS['wp_filter'][ $tag ] = array();
334 unset( $GLOBALS['merged_filters'][ $tag ] );
341 * Remove all of the hooks from a filter.
345 * @global array $wp_filter Stores all of the filters
346 * @global array $merged_filters Merges the filter hooks using this function.
348 * @param string $tag The filter to remove hooks from.
349 * @param int|bool $priority Optional. The priority number to remove. Default false.
350 * @return true True when finished.
352 function remove_all_filters( $tag, $priority = false ) {
353 global $wp_filter, $merged_filters;
355 if ( isset( $wp_filter[ $tag ]) ) {
356 if ( false === $priority ) {
357 $wp_filter[ $tag ] = array();
358 } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) {
359 $wp_filter[ $tag ][ $priority ] = array();
363 unset( $merged_filters[ $tag ] );
369 * Retrieve the name of the current filter or action.
373 * @global array $wp_current_filter Stores the list of current filters with the current one last
375 * @return string Hook name of the current filter or action.
377 function current_filter() {
378 global $wp_current_filter;
379 return end( $wp_current_filter );
383 * Retrieve the name of the current action.
387 * @return string Hook name of the current action.
389 function current_action() {
390 return current_filter();
394 * Retrieve the name of a filter currently being processed.
396 * The function current_filter() only returns the most recent filter or action
397 * being executed. did_action() returns true once the action is initially
400 * This function allows detection for any filter currently being
401 * executed (despite not being the most recent filter to fire, in the case of
402 * hooks called from hook callbacks) to be verified.
406 * @see current_filter()
408 * @global array $wp_current_filter Current filter.
410 * @param null|string $filter Optional. Filter to check. Defaults to null, which
411 * checks if any filter is currently being run.
412 * @return bool Whether the filter is currently in the stack.
414 function doing_filter( $filter = null ) {
415 global $wp_current_filter;
417 if ( null === $filter ) {
418 return ! empty( $wp_current_filter );
421 return in_array( $filter, $wp_current_filter );
425 * Retrieve the name of an action currently being processed.
429 * @param string|null $action Optional. Action to check. Defaults to null, which checks
430 * if any action is currently being run.
431 * @return bool Whether the action is currently in the stack.
433 function doing_action( $action = null ) {
434 return doing_filter( $action );
438 * Hooks a function on to a specific action.
440 * Actions are the hooks that the WordPress core launches at specific points
441 * during execution, or when specific events occur. Plugins can specify that
442 * one or more of its PHP functions are executed at these points, using the
447 * @param string $tag The name of the action to which the $function_to_add is hooked.
448 * @param callable $function_to_add The name of the function you wish to be called.
449 * @param int $priority Optional. Used to specify the order in which the functions
450 * associated with a particular action are executed. Default 10.
451 * Lower numbers correspond with earlier execution,
452 * and functions with the same priority are executed
453 * in the order in which they were added to the action.
454 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
455 * @return true Will always return true.
457 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
458 return add_filter($tag, $function_to_add, $priority, $accepted_args);
462 * Execute functions hooked on a specific action hook.
464 * This function invokes all functions attached to action hook `$tag`. It is
465 * possible to create new action hooks by simply calling this function,
466 * specifying the name of the new hook using the `$tag` parameter.
468 * You can pass extra arguments to the hooks, much like you can with apply_filters().
472 * @global array $wp_filter Stores all of the filters
473 * @global array $wp_actions Increments the amount of times action was triggered.
474 * @global array $merged_filters Merges the filter hooks using this function.
475 * @global array $wp_current_filter Stores the list of current filters with the current one last
477 * @param string $tag The name of the action to be executed.
478 * @param mixed $arg,... Optional. Additional arguments which are passed on to the
479 * functions hooked to the action. Default empty.
481 function do_action($tag, $arg = '') {
482 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
484 if ( ! isset($wp_actions[$tag]) )
485 $wp_actions[$tag] = 1;
489 // Do 'all' actions first
490 if ( isset($wp_filter['all']) ) {
491 $wp_current_filter[] = $tag;
492 $all_args = func_get_args();
493 _wp_call_all_hook($all_args);
496 if ( !isset($wp_filter[$tag]) ) {
497 if ( isset($wp_filter['all']) )
498 array_pop($wp_current_filter);
502 if ( !isset($wp_filter['all']) )
503 $wp_current_filter[] = $tag;
506 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
510 for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
511 $args[] = func_get_arg($a);
514 if ( !isset( $merged_filters[ $tag ] ) ) {
515 ksort($wp_filter[$tag]);
516 $merged_filters[ $tag ] = true;
519 reset( $wp_filter[ $tag ] );
522 foreach ( (array) current($wp_filter[$tag]) as $the_ )
523 if ( !is_null($the_['function']) )
524 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
526 } while ( next($wp_filter[$tag]) !== false );
528 array_pop($wp_current_filter);
532 * Retrieve the number of times an action is fired.
536 * @global array $wp_actions Increments the amount of times action was triggered.
538 * @param string $tag The name of the action hook.
539 * @return int The number of times action hook $tag is fired.
541 function did_action($tag) {
544 if ( ! isset( $wp_actions[ $tag ] ) )
547 return $wp_actions[$tag];
551 * Execute functions hooked on a specific action hook, specifying arguments in an array.
555 * @see do_action() This function is identical, but the arguments passed to the
556 * functions hooked to $tag< are supplied using an array.
557 * @global array $wp_filter Stores all of the filters
558 * @global array $wp_actions Increments the amount of times action was triggered.
559 * @global array $merged_filters Merges the filter hooks using this function.
560 * @global array $wp_current_filter Stores the list of current filters with the current one last
562 * @param string $tag The name of the action to be executed.
563 * @param array $args The arguments supplied to the functions hooked to `$tag`.
565 function do_action_ref_array($tag, $args) {
566 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
568 if ( ! isset($wp_actions[$tag]) )
569 $wp_actions[$tag] = 1;
573 // Do 'all' actions first
574 if ( isset($wp_filter['all']) ) {
575 $wp_current_filter[] = $tag;
576 $all_args = func_get_args();
577 _wp_call_all_hook($all_args);
580 if ( !isset($wp_filter[$tag]) ) {
581 if ( isset($wp_filter['all']) )
582 array_pop($wp_current_filter);
586 if ( !isset($wp_filter['all']) )
587 $wp_current_filter[] = $tag;
590 if ( !isset( $merged_filters[ $tag ] ) ) {
591 ksort($wp_filter[$tag]);
592 $merged_filters[ $tag ] = true;
595 reset( $wp_filter[ $tag ] );
598 foreach ( (array) current($wp_filter[$tag]) as $the_ )
599 if ( !is_null($the_['function']) )
600 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
602 } while ( next($wp_filter[$tag]) !== false );
604 array_pop($wp_current_filter);
608 * Check if any action has been registered for a hook.
612 * @see has_filter() has_action() is an alias of has_filter().
614 * @param string $tag The name of the action hook.
615 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
616 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has
617 * anything registered. When checking a specific function, the priority of that
618 * hook is returned, or false if the function is not attached. When using the
619 * $function_to_check argument, this function may return a non-boolean value
620 * that evaluates to false (e.g.) 0, so use the === operator for testing the
623 function has_action($tag, $function_to_check = false) {
624 return has_filter($tag, $function_to_check);
628 * Removes a function from a specified action hook.
630 * This function removes a function attached to a specified action hook. This
631 * method can be used to remove default functions attached to a specific filter
632 * hook and possibly replace them with a substitute.
636 * @param string $tag The action hook to which the function to be removed is hooked.
637 * @param callable $function_to_remove The name of the function which should be removed.
638 * @param int $priority Optional. The priority of the function. Default 10.
639 * @return bool Whether the function is removed.
641 function remove_action( $tag, $function_to_remove, $priority = 10 ) {
642 return remove_filter( $tag, $function_to_remove, $priority );
646 * Remove all of the hooks from an action.
650 * @param string $tag The action to remove hooks from.
651 * @param int|bool $priority The priority number to remove them from. Default false.
652 * @return true True when finished.
654 function remove_all_actions($tag, $priority = false) {
655 return remove_all_filters($tag, $priority);
659 * Fires functions attached to a deprecated filter hook.
661 * When a filter hook is deprecated, the apply_filters() call is replaced with
662 * apply_filters_deprecated(), which triggers a deprecation notice and then fires
663 * the original filter hook.
667 * @see _deprecated_hook()
669 * @param string $tag The name of the filter hook.
670 * @param array $args Array of additional function arguments to be passed to apply_filters().
671 * @param string $version The version of WordPress that deprecated the hook.
672 * @param string $replacement Optional. The hook that should have been used. Default false.
673 * @param string $message Optional. A message regarding the change. Default null.
675 function apply_filters_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
676 if ( ! has_filter( $tag ) ) {
680 _deprecated_hook( $tag, $version, $replacement, $message );
682 return apply_filters_ref_array( $tag, $args );
686 * Fires functions attached to a deprecated action hook.
688 * When an action hook is deprecated, the do_action() call is replaced with
689 * do_action_deprecated(), which triggers a deprecation notice and then fires
694 * @see _deprecated_hook()
696 * @param string $tag The name of the action hook.
697 * @param array $args Array of additional function arguments to be passed to do_action().
698 * @param string $version The version of WordPress that deprecated the hook.
699 * @param string $replacement Optional. The hook that should have been used.
700 * @param string $message Optional. A message regarding the change.
702 function do_action_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
703 if ( ! has_action( $tag ) ) {
707 _deprecated_hook( $tag, $version, $replacement, $message );
709 do_action_ref_array( $tag, $args );
713 // Functions for handling plugins.
717 * Gets the basename of a plugin.
719 * This method extracts the name of a plugin from its filename.
723 * @global array $wp_plugin_paths
725 * @param string $file The filename of plugin.
726 * @return string The name of a plugin.
728 function plugin_basename( $file ) {
729 global $wp_plugin_paths;
731 // $wp_plugin_paths contains normalized paths.
732 $file = wp_normalize_path( $file );
734 arsort( $wp_plugin_paths );
735 foreach ( $wp_plugin_paths as $dir => $realdir ) {
736 if ( strpos( $file, $realdir ) === 0 ) {
737 $file = $dir . substr( $file, strlen( $realdir ) );
741 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
742 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
744 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
745 $file = trim($file, '/');
750 * Register a plugin's real path.
752 * This is used in plugin_basename() to resolve symlinked paths.
756 * @see wp_normalize_path()
758 * @global array $wp_plugin_paths
760 * @staticvar string $wp_plugin_path
761 * @staticvar string $wpmu_plugin_path
763 * @param string $file Known path to the file.
764 * @return bool Whether the path was able to be registered.
766 function wp_register_plugin_realpath( $file ) {
767 global $wp_plugin_paths;
769 // Normalize, but store as static to avoid recalculation of a constant value
770 static $wp_plugin_path = null, $wpmu_plugin_path = null;
771 if ( ! isset( $wp_plugin_path ) ) {
772 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
773 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
776 $plugin_path = wp_normalize_path( dirname( $file ) );
777 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
779 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
783 if ( $plugin_path !== $plugin_realpath ) {
784 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
791 * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
795 * @param string $file The filename of the plugin (__FILE__).
796 * @return string the filesystem path of the directory that contains the plugin.
798 function plugin_dir_path( $file ) {
799 return trailingslashit( dirname( $file ) );
803 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
807 * @param string $file The filename of the plugin (__FILE__).
808 * @return string the URL path of the directory that contains the plugin.
810 function plugin_dir_url( $file ) {
811 return trailingslashit( plugins_url( '', $file ) );
815 * Set the activation hook for a plugin.
817 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
818 * called. In the name of this hook, PLUGINNAME is replaced with the name
819 * of the plugin, including the optional subdirectory. For example, when the
820 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
821 * the name of this hook will become 'activate_sampleplugin/sample.php'.
823 * When the plugin consists of only one file and is (as by default) located at
824 * wp-content/plugins/sample.php the name of this hook will be
825 * 'activate_sample.php'.
829 * @param string $file The filename of the plugin including the path.
830 * @param callable $function The function hooked to the 'activate_PLUGIN' action.
832 function register_activation_hook($file, $function) {
833 $file = plugin_basename($file);
834 add_action('activate_' . $file, $function);
838 * Set the deactivation hook for a plugin.
840 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
841 * called. In the name of this hook, PLUGINNAME is replaced with the name
842 * of the plugin, including the optional subdirectory. For example, when the
843 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
844 * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
846 * When the plugin consists of only one file and is (as by default) located at
847 * wp-content/plugins/sample.php the name of this hook will be
848 * 'deactivate_sample.php'.
852 * @param string $file The filename of the plugin including the path.
853 * @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
855 function register_deactivation_hook($file, $function) {
856 $file = plugin_basename($file);
857 add_action('deactivate_' . $file, $function);
861 * Set the uninstallation hook for a plugin.
863 * Registers the uninstall hook that will be called when the user clicks on the
864 * uninstall link that calls for the plugin to uninstall itself. The link won't
865 * be active unless the plugin hooks into the action.
867 * The plugin should not run arbitrary code outside of functions, when
868 * registering the uninstall hook. In order to run using the hook, the plugin
869 * will have to be included, which means that any code laying outside of a
870 * function will be run during the uninstall process. The plugin should not
871 * hinder the uninstall process.
873 * If the plugin can not be written without running code within the plugin, then
874 * the plugin should create a file named 'uninstall.php' in the base plugin
875 * folder. This file will be called, if it exists, during the uninstall process
876 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
877 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
882 * @param string $file Plugin file.
883 * @param callable $callback The callback to run when the hook is called. Must be
884 * a static method or function.
886 function register_uninstall_hook( $file, $callback ) {
887 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
888 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
893 * The option should not be autoloaded, because it is not needed in most
894 * cases. Emphasis should be put on using the 'uninstall.php' way of
895 * uninstalling the plugin.
897 $uninstallable_plugins = (array) get_option('uninstall_plugins');
898 $uninstallable_plugins[plugin_basename($file)] = $callback;
900 update_option('uninstall_plugins', $uninstallable_plugins);
904 * Call the 'all' hook, which will process the functions hooked into it.
906 * The 'all' hook passes all of the arguments or parameters that were used for
907 * the hook, which this function was called for.
909 * This function is used internally for apply_filters(), do_action(), and
910 * do_action_ref_array() and is not meant to be used from outside those
911 * functions. This function does not check for the existence of the all hook, so
912 * it will fail unless the all hook exists prior to this function call.
917 * @global array $wp_filter Stores all of the filters
919 * @param array $args The collected parameters from the hook that was called.
921 function _wp_call_all_hook($args) {
924 reset( $wp_filter['all'] );
926 foreach ( (array) current($wp_filter['all']) as $the_ )
927 if ( !is_null($the_['function']) )
928 call_user_func_array($the_['function'], $args);
930 } while ( next($wp_filter['all']) !== false );
934 * Build Unique ID for storage and retrieval.
936 * The old way to serialize the callback caused issues and this function is the
937 * solution. It works by checking for objects and creating a new property in
938 * the class to keep track of the object and new objects of the same class that
941 * It also allows for the removal of actions and filters for objects after they
942 * change class properties. It is possible to include the property $wp_filter_id
943 * in your class and set it to "null" or a number to bypass the workaround.
944 * However this will prevent you from adding new classes and any new classes
945 * will overwrite the previous hook by the same class.
947 * Functions and static method callbacks are just returned as strings and
948 * shouldn't have any speed penalty.
950 * @link https://core.trac.wordpress.org/ticket/3875
955 * @global array $wp_filter Storage for all of the filters and actions.
956 * @staticvar int $filter_id_count
958 * @param string $tag Used in counting how many hooks were applied
959 * @param callable $function Used for creating unique id
960 * @param int|bool $priority Used in counting how many hooks were applied. If === false
961 * and $function is an object reference, we return the unique
962 * id only if it already has one, false otherwise.
963 * @return string|false Unique ID for usage as array key or false if $priority === false
964 * and $function is an object reference, and it does not already have
967 function _wp_filter_build_unique_id($tag, $function, $priority) {
969 static $filter_id_count = 0;
971 if ( is_string($function) )
974 if ( is_object($function) ) {
975 // Closures are currently implemented as objects
976 $function = array( $function, '' );
978 $function = (array) $function;
981 if (is_object($function[0]) ) {
982 // Object Class Calling
983 if ( function_exists('spl_object_hash') ) {
984 return spl_object_hash($function[0]) . $function[1];
986 $obj_idx = get_class($function[0]).$function[1];
987 if ( !isset($function[0]->wp_filter_id) ) {
988 if ( false === $priority )
990 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
991 $function[0]->wp_filter_id = $filter_id_count;
994 $obj_idx .= $function[0]->wp_filter_id;
999 } elseif ( is_string( $function[0] ) ) {
1001 return $function[0] . '::' . $function[1];