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.
23 * Hooks a function or method to a specific filter action.
25 * Filters are the hooks that WordPress launches to modify text of various types
26 * before adding it to the database or sending it to the browser screen. Plugins
27 * can specify that one or more of its PHP functions is executed to
28 * modify specific types of text at these times, using the Filter API.
30 * To use the API, the following code should be used to bind a callback to the
34 * function example_hook($example) { echo $example; }
35 * add_filter('example_filter', 'example_hook');
38 * In WordPress 1.5.1+, hooked functions can take extra arguments that are set
39 * when the matching do_action() or apply_filters() call is run. The
40 * $accepted_args allow for calling functions only when the number of args
41 * match. Hooked functions can take extra arguments that are set when the
42 * matching do_action() or apply_filters() call is run. For example, the action
43 * comment_id_not_found will pass any functions that hook onto it the ID of the
46 * <strong>Note:</strong> the function will return true no matter if the
47 * function was hooked fails or not. There are no checks for whether the
48 * function exists beforehand and no checks to whether the <tt>$function_to_add
49 * is even a string. It is up to you to take care and this is done for
50 * optimization purposes, so everything is as quick as possible.
55 * @global array $wp_filter Stores all of the filters added in the form of
56 * wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
57 * @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.
59 * @param string $tag The name of the filter to hook the $function_to_add to.
60 * @param callback $function_to_add The name of the function to be called when the filter is applied.
61 * @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.
62 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
63 * @return boolean true
65 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
66 global $wp_filter, $merged_filters;
68 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
69 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
70 unset( $merged_filters[ $tag ] );
75 * Check if any filter has been registered for a hook.
80 * @global array $wp_filter Stores all of the filters
82 * @param string $tag The name of the filter hook.
83 * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
84 * @return int|boolean Optionally returns the priority on that hook for the specified function.
86 function has_filter($tag, $function_to_check = false) {
89 $has = !empty($wp_filter[$tag]);
90 if ( false === $function_to_check || false == $has )
93 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
96 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
97 if ( isset($wp_filter[$tag][$priority][$idx]) )
105 * Call the functions added to a filter hook.
107 * The callback functions attached to filter hook $tag are invoked by calling
108 * this function. This function can be used to create a new filter hook by
109 * simply calling this function with the name of the new hook specified using
110 * the $tag parameter.
112 * The function allows for additional arguments to be added and passed to hooks.
114 * function example_hook($string, $arg1, $arg2)
119 * $value = apply_filters('example_filter', 'filter me', 'arg1', 'arg2');
125 * @global array $wp_filter Stores all of the filters
126 * @global array $merged_filters Merges the filter hooks using this function.
127 * @global array $wp_current_filter stores the list of current filters with the current one last
129 * @param string $tag The name of the filter hook.
130 * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
131 * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
132 * @return mixed The filtered value after all hooked functions are applied to it.
134 function apply_filters($tag, $value) {
135 global $wp_filter, $merged_filters, $wp_current_filter;
138 $wp_current_filter[] = $tag;
140 // Do 'all' actions first
141 if ( isset($wp_filter['all']) ) {
142 $args = func_get_args();
143 _wp_call_all_hook($args);
146 if ( !isset($wp_filter[$tag]) ) {
147 array_pop($wp_current_filter);
152 if ( !isset( $merged_filters[ $tag ] ) ) {
153 ksort($wp_filter[$tag]);
154 $merged_filters[ $tag ] = true;
157 reset( $wp_filter[ $tag ] );
160 $args = func_get_args();
163 foreach( (array) current($wp_filter[$tag]) as $the_ )
164 if ( !is_null($the_['function']) ){
166 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
169 } while ( next($wp_filter[$tag]) !== false );
171 array_pop( $wp_current_filter );
177 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
179 * @see apply_filters() This function is identical, but the arguments passed to the
180 * functions hooked to <tt>$tag</tt> are supplied using an array.
185 * @global array $wp_filter Stores all of the filters
186 * @global array $merged_filters Merges the filter hooks using this function.
187 * @global array $wp_current_filter stores the list of current filters with the current one last
189 * @param string $tag The name of the filter hook.
190 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
191 * @return mixed The filtered value after all hooked functions are applied to it.
193 function apply_filters_ref_array($tag, $args) {
194 global $wp_filter, $merged_filters, $wp_current_filter;
196 $wp_current_filter[] = $tag;
198 // Do 'all' actions first
199 if ( isset($wp_filter['all']) ) {
200 $all_args = func_get_args();
201 _wp_call_all_hook($all_args);
204 if ( !isset($wp_filter[$tag]) ) {
205 array_pop($wp_current_filter);
210 if ( !isset( $merged_filters[ $tag ] ) ) {
211 ksort($wp_filter[$tag]);
212 $merged_filters[ $tag ] = true;
215 reset( $wp_filter[ $tag ] );
218 foreach( (array) current($wp_filter[$tag]) as $the_ )
219 if ( !is_null($the_['function']) )
220 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
222 } while ( next($wp_filter[$tag]) !== false );
224 array_pop( $wp_current_filter );
230 * Removes a function from a specified filter hook.
232 * This function removes a function attached to a specified filter hook. This
233 * method can be used to remove default functions attached to a specific filter
234 * hook and possibly replace them with a substitute.
236 * To remove a hook, the $function_to_remove and $priority arguments must match
237 * when the hook was added. This goes for both filters and actions. No warning
238 * will be given on removal failure.
244 * @param string $tag The filter hook to which the function to be removed is hooked.
245 * @param callback $function_to_remove The name of the function which should be removed.
246 * @param int $priority optional. The priority of the function (default: 10).
247 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
248 * @return boolean Whether the function existed before it was removed.
250 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
251 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
253 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
256 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
257 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
258 unset($GLOBALS['wp_filter'][$tag][$priority]);
259 unset($GLOBALS['merged_filters'][$tag]);
266 * Remove all of the hooks from a filter.
270 * @param string $tag The filter to remove hooks from.
271 * @param int $priority The priority number to remove.
272 * @return bool True when finished.
274 function remove_all_filters($tag, $priority = false) {
275 global $wp_filter, $merged_filters;
277 if( isset($wp_filter[$tag]) ) {
278 if( false !== $priority && isset($wp_filter[$tag][$priority]) )
279 unset($wp_filter[$tag][$priority]);
281 unset($wp_filter[$tag]);
284 if( isset($merged_filters[$tag]) )
285 unset($merged_filters[$tag]);
291 * Retrieve the name of the current filter or action.
297 * @return string Hook name of the current filter or action.
299 function current_filter() {
300 global $wp_current_filter;
301 return end( $wp_current_filter );
306 * Hooks a function on to a specific action.
308 * Actions are the hooks that the WordPress core launches at specific points
309 * during execution, or when specific events occur. Plugins can specify that
310 * one or more of its PHP functions are executed at these points, using the
313 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
319 * @param string $tag The name of the action to which the $function_to_add is hooked.
320 * @param callback $function_to_add The name of the function you wish to be called.
321 * @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.
322 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
324 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
325 return add_filter($tag, $function_to_add, $priority, $accepted_args);
330 * Execute functions hooked on a specific action hook.
332 * This function invokes all functions attached to action hook $tag. It is
333 * possible to create new action hooks by simply calling this function,
334 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
336 * You can pass extra arguments to the hooks, much like you can with
339 * @see apply_filters() This function works similar with the exception that
340 * nothing is returned and only the functions or methods are called.
345 * @global array $wp_filter Stores all of the filters
346 * @global array $wp_actions Increments the amount of times action was triggered.
348 * @param string $tag The name of the action to be executed.
349 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
350 * @return null Will return null if $tag does not exist in $wp_filter array
352 function do_action($tag, $arg = '') {
353 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
355 if ( ! isset($wp_actions) )
356 $wp_actions = array();
358 if ( ! isset($wp_actions[$tag]) )
359 $wp_actions[$tag] = 1;
363 $wp_current_filter[] = $tag;
365 // Do 'all' actions first
366 if ( isset($wp_filter['all']) ) {
367 $all_args = func_get_args();
368 _wp_call_all_hook($all_args);
371 if ( !isset($wp_filter[$tag]) ) {
372 array_pop($wp_current_filter);
377 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
381 for ( $a = 2; $a < func_num_args(); $a++ )
382 $args[] = func_get_arg($a);
385 if ( !isset( $merged_filters[ $tag ] ) ) {
386 ksort($wp_filter[$tag]);
387 $merged_filters[ $tag ] = true;
390 reset( $wp_filter[ $tag ] );
393 foreach ( (array) current($wp_filter[$tag]) as $the_ )
394 if ( !is_null($the_['function']) )
395 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
397 } while ( next($wp_filter[$tag]) !== false );
399 array_pop($wp_current_filter);
403 * Retrieve the number times an action is fired.
408 * @global array $wp_actions Increments the amount of times action was triggered.
410 * @param string $tag The name of the action hook.
411 * @return int The number of times action hook <tt>$tag</tt> is fired
413 function did_action($tag) {
416 if ( ! isset( $wp_actions ) || ! isset( $wp_actions[$tag] ) )
419 return $wp_actions[$tag];
423 * Execute functions hooked on a specific action hook, specifying arguments in an array.
425 * @see do_action() This function is identical, but the arguments passed to the
426 * functions hooked to <tt>$tag</tt> are supplied using an array.
431 * @global array $wp_filter Stores all of the filters
432 * @global array $wp_actions Increments the amount of times action was triggered.
434 * @param string $tag The name of the action to be executed.
435 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
436 * @return null Will return null if $tag does not exist in $wp_filter array
438 function do_action_ref_array($tag, $args) {
439 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
441 if ( ! isset($wp_actions) )
442 $wp_actions = array();
444 if ( ! isset($wp_actions[$tag]) )
445 $wp_actions[$tag] = 1;
449 $wp_current_filter[] = $tag;
451 // Do 'all' actions first
452 if ( isset($wp_filter['all']) ) {
453 $all_args = func_get_args();
454 _wp_call_all_hook($all_args);
457 if ( !isset($wp_filter[$tag]) ) {
458 array_pop($wp_current_filter);
463 if ( !isset( $merged_filters[ $tag ] ) ) {
464 ksort($wp_filter[$tag]);
465 $merged_filters[ $tag ] = true;
468 reset( $wp_filter[ $tag ] );
471 foreach( (array) current($wp_filter[$tag]) as $the_ )
472 if ( !is_null($the_['function']) )
473 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
475 } while ( next($wp_filter[$tag]) !== false );
477 array_pop($wp_current_filter);
481 * Check if any action has been registered for a hook.
486 * @see has_filter() has_action() is an alias of has_filter().
488 * @param string $tag The name of the action hook.
489 * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
490 * @return int|boolean Optionally returns the priority on that hook for the specified function.
492 function has_action($tag, $function_to_check = false) {
493 return has_filter($tag, $function_to_check);
497 * Removes a function from a specified action hook.
499 * This function removes a function attached to a specified action hook. This
500 * method can be used to remove default functions attached to a specific filter
501 * hook and possibly replace them with a substitute.
507 * @param string $tag The action hook to which the function to be removed is hooked.
508 * @param callback $function_to_remove The name of the function which should be removed.
509 * @param int $priority optional The priority of the function (default: 10).
510 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
511 * @return boolean Whether the function is removed.
513 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
514 return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
518 * Remove all of the hooks from an action.
522 * @param string $tag The action to remove hooks from.
523 * @param int $priority The priority number to remove them from.
524 * @return bool True when finished.
526 function remove_all_actions($tag, $priority = false) {
527 return remove_all_filters($tag, $priority);
531 // Functions for handling plugins.
535 * Gets the basename of a plugin.
537 * This method extracts the name of a plugin from its filename.
545 * @param string $file The filename of plugin.
546 * @return string The name of a plugin.
547 * @uses WP_PLUGIN_DIR
549 function plugin_basename($file) {
550 $file = str_replace('\\','/',$file); // sanitize for Win32 installs
551 $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
552 $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
553 $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
554 $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
555 $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
556 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
557 $file = trim($file, '/');
562 * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
567 * @param string $file The filename of the plugin (__FILE__)
568 * @return string the filesystem path of the directory that contains the plugin
570 function plugin_dir_path( $file ) {
571 return trailingslashit( dirname( $file ) );
575 * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
580 * @param string $file The filename of the plugin (__FILE__)
581 * @return string the URL path of the directory that contains the plugin
583 function plugin_dir_url( $file ) {
584 return trailingslashit( plugins_url( '', $file ) );
588 * Set the activation hook for a plugin.
590 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
591 * activated. In the name of this hook, PLUGINNAME is replaced with the name of
592 * the plugin, including the optional subdirectory. For example, when the plugin
593 * is located in wp-content/plugin/sampleplugin/sample.php, then the name of
594 * this hook will become 'activate_sampleplugin/sample.php'. When the plugin
595 * consists of only one file and is (as by default) located at
596 * wp-content/plugin/sample.php the name of this hook will be
597 * 'activate_sample.php'.
603 * @param string $file The filename of the plugin including the path.
604 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
606 function register_activation_hook($file, $function) {
607 $file = plugin_basename($file);
608 add_action('activate_' . $file, $function);
612 * Set the deactivation hook for a plugin.
614 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
615 * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
616 * of the plugin, including the optional subdirectory. For example, when the
617 * plugin is located in wp-content/plugin/sampleplugin/sample.php, then
618 * the name of this hook will become 'activate_sampleplugin/sample.php'.
620 * When the plugin consists of only one file and is (as by default) located at
621 * wp-content/plugin/sample.php the name of this hook will be
622 * 'activate_sample.php'.
628 * @param string $file The filename of the plugin including the path.
629 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
631 function register_deactivation_hook($file, $function) {
632 $file = plugin_basename($file);
633 add_action('deactivate_' . $file, $function);
637 * Set the uninstallation hook for a plugin.
639 * Registers the uninstall hook that will be called when the user clicks on the
640 * uninstall link that calls for the plugin to uninstall itself. The link won't
641 * be active unless the plugin hooks into the action.
643 * The plugin should not run arbitrary code outside of functions, when
644 * registering the uninstall hook. In order to run using the hook, the plugin
645 * will have to be included, which means that any code laying outside of a
646 * function will be run during the uninstall process. The plugin should not
647 * hinder the uninstall process.
649 * If the plugin can not be written without running code within the plugin, then
650 * the plugin should create a file named 'uninstall.php' in the base plugin
651 * folder. This file will be called, if it exists, during the uninstall process
652 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
653 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
658 * @param string $file
659 * @param callback $callback The callback to run when the hook is called. Must be a static method or function.
661 function register_uninstall_hook( $file, $callback ) {
662 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
663 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
667 // The option should not be autoloaded, because it is not needed in most
668 // cases. Emphasis should be put on using the 'uninstall.php' way of
669 // uninstalling the plugin.
670 $uninstallable_plugins = (array) get_option('uninstall_plugins');
671 $uninstallable_plugins[plugin_basename($file)] = $callback;
672 update_option('uninstall_plugins', $uninstallable_plugins);
676 * Calls the 'all' hook, which will process the functions hooked into it.
678 * The 'all' hook passes all of the arguments or parameters that were used for
679 * the hook, which this function was called for.
681 * This function is used internally for apply_filters(), do_action(), and
682 * do_action_ref_array() and is not meant to be used from outside those
683 * functions. This function does not check for the existence of the all hook, so
684 * it will fail unless the all hook exists prior to this function call.
691 * @uses $wp_filter Used to process all of the functions in the 'all' hook
693 * @param array $args The collected parameters from the hook that was called.
694 * @param string $hook Optional. The hook name that was used to call the 'all' hook.
696 function _wp_call_all_hook($args) {
699 reset( $wp_filter['all'] );
701 foreach( (array) current($wp_filter['all']) as $the_ )
702 if ( !is_null($the_['function']) )
703 call_user_func_array($the_['function'], $args);
705 } while ( next($wp_filter['all']) !== false );
709 * Build Unique ID for storage and retrieval.
711 * The old way to serialize the callback caused issues and this function is the
712 * solution. It works by checking for objects and creating an a new property in
713 * the class to keep track of the object and new objects of the same class that
716 * It also allows for the removal of actions and filters for objects after they
717 * change class properties. It is possible to include the property $wp_filter_id
718 * in your class and set it to "null" or a number to bypass the workaround.
719 * However this will prevent you from adding new classes and any new classes
720 * will overwrite the previous hook by the same class.
722 * Functions and static method callbacks are just returned as strings and
723 * shouldn't have any speed penalty.
729 * @link http://trac.wordpress.org/ticket/3875
731 * @global array $wp_filter Storage for all of the filters and actions
732 * @param string $tag Used in counting how many hooks were applied
733 * @param callback $function Used for creating unique id
734 * @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.
735 * @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 uniqe id.
737 function _wp_filter_build_unique_id($tag, $function, $priority) {
739 static $filter_id_count = 0;
741 if ( is_string($function) )
744 if ( is_object($function) ) {
745 // Closures are currently implemented as objects
746 $function = array( $function, '' );
748 $function = (array) $function;
751 if (is_object($function[0]) ) {
752 // Object Class Calling
753 if ( function_exists('spl_object_hash') ) {
754 return spl_object_hash($function[0]) . $function[1];
756 $obj_idx = get_class($function[0]).$function[1];
757 if ( !isset($function[0]->wp_filter_id) ) {
758 if ( false === $priority )
760 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
761 $function[0]->wp_filter_id = $filter_id_count;
764 $obj_idx .= $function[0]->wp_filter_id;
769 } else if ( is_string($function[0]) ) {
771 return $function[0].$function[1];