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;
139 // Do 'all' actions first
140 if ( isset($wp_filter['all']) ) {
141 $wp_current_filter[] = $tag;
142 $args = func_get_args();
143 _wp_call_all_hook($args);
146 if ( !isset($wp_filter[$tag]) ) {
147 if ( isset($wp_filter['all']) )
148 array_pop($wp_current_filter);
152 if ( !isset($wp_filter['all']) )
153 $wp_current_filter[] = $tag;
156 if ( !isset( $merged_filters[ $tag ] ) ) {
157 ksort($wp_filter[$tag]);
158 $merged_filters[ $tag ] = true;
161 reset( $wp_filter[ $tag ] );
164 $args = func_get_args();
167 foreach( (array) current($wp_filter[$tag]) as $the_ )
168 if ( !is_null($the_['function']) ){
170 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
173 } while ( next($wp_filter[$tag]) !== false );
175 array_pop( $wp_current_filter );
181 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
183 * @see apply_filters() This function is identical, but the arguments passed to the
184 * functions hooked to <tt>$tag</tt> are supplied using an array.
189 * @global array $wp_filter Stores all of the filters
190 * @global array $merged_filters Merges the filter hooks using this function.
191 * @global array $wp_current_filter stores the list of current filters with the current one last
193 * @param string $tag The name of the filter hook.
194 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
195 * @return mixed The filtered value after all hooked functions are applied to it.
197 function apply_filters_ref_array($tag, $args) {
198 global $wp_filter, $merged_filters, $wp_current_filter;
200 // Do 'all' actions first
201 if ( isset($wp_filter['all']) ) {
202 $wp_current_filter[] = $tag;
203 $all_args = func_get_args();
204 _wp_call_all_hook($all_args);
207 if ( !isset($wp_filter[$tag]) ) {
208 if ( isset($wp_filter['all']) )
209 array_pop($wp_current_filter);
213 if ( !isset($wp_filter['all']) )
214 $wp_current_filter[] = $tag;
217 if ( !isset( $merged_filters[ $tag ] ) ) {
218 ksort($wp_filter[$tag]);
219 $merged_filters[ $tag ] = true;
222 reset( $wp_filter[ $tag ] );
225 foreach( (array) current($wp_filter[$tag]) as $the_ )
226 if ( !is_null($the_['function']) )
227 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
229 } while ( next($wp_filter[$tag]) !== false );
231 array_pop( $wp_current_filter );
237 * Removes a function from a specified filter hook.
239 * This function removes a function attached to a specified filter hook. This
240 * method can be used to remove default functions attached to a specific filter
241 * hook and possibly replace them with a substitute.
243 * To remove a hook, the $function_to_remove and $priority arguments must match
244 * when the hook was added. This goes for both filters and actions. No warning
245 * will be given on removal failure.
251 * @param string $tag The filter hook to which the function to be removed is hooked.
252 * @param callback $function_to_remove The name of the function which should be removed.
253 * @param int $priority optional. The priority of the function (default: 10).
254 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
255 * @return boolean Whether the function existed before it was removed.
257 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
258 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
260 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
263 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
264 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
265 unset($GLOBALS['wp_filter'][$tag][$priority]);
266 unset($GLOBALS['merged_filters'][$tag]);
273 * Remove all of the hooks from a filter.
277 * @param string $tag The filter to remove hooks from.
278 * @param int $priority The priority number to remove.
279 * @return bool True when finished.
281 function remove_all_filters($tag, $priority = false) {
282 global $wp_filter, $merged_filters;
284 if( isset($wp_filter[$tag]) ) {
285 if( false !== $priority && isset($wp_filter[$tag][$priority]) )
286 unset($wp_filter[$tag][$priority]);
288 unset($wp_filter[$tag]);
291 if( isset($merged_filters[$tag]) )
292 unset($merged_filters[$tag]);
298 * Retrieve the name of the current filter or action.
304 * @return string Hook name of the current filter or action.
306 function current_filter() {
307 global $wp_current_filter;
308 return end( $wp_current_filter );
313 * Hooks a function on to a specific action.
315 * Actions are the hooks that the WordPress core launches at specific points
316 * during execution, or when specific events occur. Plugins can specify that
317 * one or more of its PHP functions are executed at these points, using the
320 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
326 * @param string $tag The name of the action to which the $function_to_add is hooked.
327 * @param callback $function_to_add The name of the function you wish to be called.
328 * @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.
329 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
331 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
332 return add_filter($tag, $function_to_add, $priority, $accepted_args);
337 * Execute functions hooked on a specific action hook.
339 * This function invokes all functions attached to action hook $tag. It is
340 * possible to create new action hooks by simply calling this function,
341 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
343 * You can pass extra arguments to the hooks, much like you can with
346 * @see apply_filters() This function works similar with the exception that
347 * nothing is returned and only the functions or methods are called.
352 * @global array $wp_filter Stores all of the filters
353 * @global array $wp_actions Increments the amount of times action was triggered.
355 * @param string $tag The name of the action to be executed.
356 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
357 * @return null Will return null if $tag does not exist in $wp_filter array
359 function do_action($tag, $arg = '') {
360 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
362 if ( ! isset($wp_actions) )
363 $wp_actions = array();
365 if ( ! isset($wp_actions[$tag]) )
366 $wp_actions[$tag] = 1;
370 // Do 'all' actions first
371 if ( isset($wp_filter['all']) ) {
372 $wp_current_filter[] = $tag;
373 $all_args = func_get_args();
374 _wp_call_all_hook($all_args);
377 if ( !isset($wp_filter[$tag]) ) {
378 if ( isset($wp_filter['all']) )
379 array_pop($wp_current_filter);
383 if ( !isset($wp_filter['all']) )
384 $wp_current_filter[] = $tag;
387 if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
391 for ( $a = 2; $a < func_num_args(); $a++ )
392 $args[] = func_get_arg($a);
395 if ( !isset( $merged_filters[ $tag ] ) ) {
396 ksort($wp_filter[$tag]);
397 $merged_filters[ $tag ] = true;
400 reset( $wp_filter[ $tag ] );
403 foreach ( (array) current($wp_filter[$tag]) as $the_ )
404 if ( !is_null($the_['function']) )
405 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
407 } while ( next($wp_filter[$tag]) !== false );
409 array_pop($wp_current_filter);
413 * Retrieve the number times an action is fired.
418 * @global array $wp_actions Increments the amount of times action was triggered.
420 * @param string $tag The name of the action hook.
421 * @return int The number of times action hook <tt>$tag</tt> is fired
423 function did_action($tag) {
426 if ( ! isset( $wp_actions ) || ! isset( $wp_actions[$tag] ) )
429 return $wp_actions[$tag];
433 * Execute functions hooked on a specific action hook, specifying arguments in an array.
435 * @see do_action() This function is identical, but the arguments passed to the
436 * functions hooked to <tt>$tag</tt> are supplied using an array.
441 * @global array $wp_filter Stores all of the filters
442 * @global array $wp_actions Increments the amount of times action was triggered.
444 * @param string $tag The name of the action to be executed.
445 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
446 * @return null Will return null if $tag does not exist in $wp_filter array
448 function do_action_ref_array($tag, $args) {
449 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
451 if ( ! isset($wp_actions) )
452 $wp_actions = array();
454 if ( ! isset($wp_actions[$tag]) )
455 $wp_actions[$tag] = 1;
459 // Do 'all' actions first
460 if ( isset($wp_filter['all']) ) {
461 $wp_current_filter[] = $tag;
462 $all_args = func_get_args();
463 _wp_call_all_hook($all_args);
466 if ( !isset($wp_filter[$tag]) ) {
467 if ( isset($wp_filter['all']) )
468 array_pop($wp_current_filter);
472 if ( !isset($wp_filter['all']) )
473 $wp_current_filter[] = $tag;
476 if ( !isset( $merged_filters[ $tag ] ) ) {
477 ksort($wp_filter[$tag]);
478 $merged_filters[ $tag ] = true;
481 reset( $wp_filter[ $tag ] );
484 foreach( (array) current($wp_filter[$tag]) as $the_ )
485 if ( !is_null($the_['function']) )
486 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
488 } while ( next($wp_filter[$tag]) !== false );
490 array_pop($wp_current_filter);
494 * Check if any action has been registered for a hook.
499 * @see has_filter() has_action() is an alias of has_filter().
501 * @param string $tag The name of the action hook.
502 * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
503 * @return int|boolean Optionally returns the priority on that hook for the specified function.
505 function has_action($tag, $function_to_check = false) {
506 return has_filter($tag, $function_to_check);
510 * Removes a function from a specified action hook.
512 * This function removes a function attached to a specified action hook. This
513 * method can be used to remove default functions attached to a specific filter
514 * hook and possibly replace them with a substitute.
520 * @param string $tag The action hook to which the function to be removed is hooked.
521 * @param callback $function_to_remove The name of the function which should be removed.
522 * @param int $priority optional The priority of the function (default: 10).
523 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
524 * @return boolean Whether the function is removed.
526 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
527 return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
531 * Remove all of the hooks from an action.
535 * @param string $tag The action to remove hooks from.
536 * @param int $priority The priority number to remove them from.
537 * @return bool True when finished.
539 function remove_all_actions($tag, $priority = false) {
540 return remove_all_filters($tag, $priority);
544 // Functions for handling plugins.
548 * Gets the basename of a plugin.
550 * This method extracts the name of a plugin from its filename.
558 * @param string $file The filename of plugin.
559 * @return string The name of a plugin.
560 * @uses WP_PLUGIN_DIR
562 function plugin_basename($file) {
563 $file = str_replace('\\','/',$file); // sanitize for Win32 installs
564 $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
565 $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
566 $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
567 $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
568 $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
569 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
570 $file = trim($file, '/');
575 * Gets the filesystem 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 filesystem path of the directory that contains the plugin
583 function plugin_dir_path( $file ) {
584 return trailingslashit( dirname( $file ) );
588 * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
593 * @param string $file The filename of the plugin (__FILE__)
594 * @return string the URL path of the directory that contains the plugin
596 function plugin_dir_url( $file ) {
597 return trailingslashit( plugins_url( '', $file ) );
601 * Set the activation hook for a plugin.
603 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
604 * activated. In the name of this hook, PLUGINNAME is replaced with the name of
605 * the plugin, including the optional subdirectory. For example, when the plugin
606 * is located in wp-content/plugin/sampleplugin/sample.php, then the name of
607 * this hook will become 'activate_sampleplugin/sample.php'. When the plugin
608 * consists of only one file and is (as by default) located at
609 * wp-content/plugin/sample.php the name of this hook will be
610 * 'activate_sample.php'.
616 * @param string $file The filename of the plugin including the path.
617 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
619 function register_activation_hook($file, $function) {
620 $file = plugin_basename($file);
621 add_action('activate_' . $file, $function);
625 * Set the deactivation hook for a plugin.
627 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
628 * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
629 * of the plugin, including the optional subdirectory. For example, when the
630 * plugin is located in wp-content/plugin/sampleplugin/sample.php, then
631 * the name of this hook will become 'activate_sampleplugin/sample.php'.
633 * When the plugin consists of only one file and is (as by default) located at
634 * wp-content/plugin/sample.php the name of this hook will be
635 * 'activate_sample.php'.
641 * @param string $file The filename of the plugin including the path.
642 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
644 function register_deactivation_hook($file, $function) {
645 $file = plugin_basename($file);
646 add_action('deactivate_' . $file, $function);
650 * Set the uninstallation hook for a plugin.
652 * Registers the uninstall hook that will be called when the user clicks on the
653 * uninstall link that calls for the plugin to uninstall itself. The link won't
654 * be active unless the plugin hooks into the action.
656 * The plugin should not run arbitrary code outside of functions, when
657 * registering the uninstall hook. In order to run using the hook, the plugin
658 * will have to be included, which means that any code laying outside of a
659 * function will be run during the uninstall process. The plugin should not
660 * hinder the uninstall process.
662 * If the plugin can not be written without running code within the plugin, then
663 * the plugin should create a file named 'uninstall.php' in the base plugin
664 * folder. This file will be called, if it exists, during the uninstall process
665 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
666 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
671 * @param string $file
672 * @param callback $callback The callback to run when the hook is called. Must be a static method or function.
674 function register_uninstall_hook( $file, $callback ) {
675 if ( is_array( $callback ) && is_object( $callback[0] ) ) {
676 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
680 // The option should not be autoloaded, because it is not needed in most
681 // cases. Emphasis should be put on using the 'uninstall.php' way of
682 // uninstalling the plugin.
683 $uninstallable_plugins = (array) get_option('uninstall_plugins');
684 $uninstallable_plugins[plugin_basename($file)] = $callback;
685 update_option('uninstall_plugins', $uninstallable_plugins);
689 * Calls the 'all' hook, which will process the functions hooked into it.
691 * The 'all' hook passes all of the arguments or parameters that were used for
692 * the hook, which this function was called for.
694 * This function is used internally for apply_filters(), do_action(), and
695 * do_action_ref_array() and is not meant to be used from outside those
696 * functions. This function does not check for the existence of the all hook, so
697 * it will fail unless the all hook exists prior to this function call.
704 * @uses $wp_filter Used to process all of the functions in the 'all' hook
706 * @param array $args The collected parameters from the hook that was called.
707 * @param string $hook Optional. The hook name that was used to call the 'all' hook.
709 function _wp_call_all_hook($args) {
712 reset( $wp_filter['all'] );
714 foreach( (array) current($wp_filter['all']) as $the_ )
715 if ( !is_null($the_['function']) )
716 call_user_func_array($the_['function'], $args);
718 } while ( next($wp_filter['all']) !== false );
722 * Build Unique ID for storage and retrieval.
724 * The old way to serialize the callback caused issues and this function is the
725 * solution. It works by checking for objects and creating an a new property in
726 * the class to keep track of the object and new objects of the same class that
729 * It also allows for the removal of actions and filters for objects after they
730 * change class properties. It is possible to include the property $wp_filter_id
731 * in your class and set it to "null" or a number to bypass the workaround.
732 * However this will prevent you from adding new classes and any new classes
733 * will overwrite the previous hook by the same class.
735 * Functions and static method callbacks are just returned as strings and
736 * shouldn't have any speed penalty.
742 * @link http://trac.wordpress.org/ticket/3875
744 * @global array $wp_filter Storage for all of the filters and actions
745 * @param string $tag Used in counting how many hooks were applied
746 * @param callback $function Used for creating unique id
747 * @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.
748 * @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.
750 function _wp_filter_build_unique_id($tag, $function, $priority) {
752 static $filter_id_count = 0;
754 if ( is_string($function) )
757 if ( is_object($function) ) {
758 // Closures are currently implemented as objects
759 $function = array( $function, '' );
761 $function = (array) $function;
764 if (is_object($function[0]) ) {
765 // Object Class Calling
766 if ( function_exists('spl_object_hash') ) {
767 return spl_object_hash($function[0]) . $function[1];
769 $obj_idx = get_class($function[0]).$function[1];
770 if ( !isset($function[0]->wp_filter_id) ) {
771 if ( false === $priority )
773 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
774 $function[0]->wp_filter_id = $filter_id_count;
777 $obj_idx .= $function[0]->wp_filter_id;
782 } else if ( is_string($function[0]) ) {
784 return $function[0].$function[1];