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 * Removes a function from a specified filter hook.
179 * This function removes a function attached to a specified filter hook. This
180 * method can be used to remove default functions attached to a specific filter
181 * hook and possibly replace them with a substitute.
183 * To remove a hook, the $function_to_remove and $priority arguments must match
184 * when the hook was added. This goes for both filters and actions. No warning
185 * will be given on removal failure.
191 * @param string $tag The filter hook to which the function to be removed is hooked.
192 * @param callback $function_to_remove The name of the function which should be removed.
193 * @param int $priority optional. The priority of the function (default: 10).
194 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
195 * @return boolean Whether the function existed before it was removed.
197 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
198 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
200 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
203 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
204 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
205 unset($GLOBALS['wp_filter'][$tag][$priority]);
206 unset($GLOBALS['merged_filters'][$tag]);
213 * Remove all of the hooks from a filter.
217 * @param string $tag The filter to remove hooks from.
218 * @param int $priority The priority number to remove.
219 * @return bool True when finished.
221 function remove_all_filters($tag, $priority = false) {
222 global $wp_filter, $merged_filters;
224 if( isset($wp_filter[$tag]) ) {
225 if( false !== $priority && isset($$wp_filter[$tag][$priority]) )
226 unset($wp_filter[$tag][$priority]);
228 unset($wp_filter[$tag]);
231 if( isset($merged_filters[$tag]) )
232 unset($merged_filters[$tag]);
238 * Retrieve the name of the current filter or action.
244 * @return string Hook name of the current filter or action.
246 function current_filter() {
247 global $wp_current_filter;
248 return end( $wp_current_filter );
253 * Hooks a function on to a specific action.
255 * Actions are the hooks that the WordPress core launches at specific points
256 * during execution, or when specific events occur. Plugins can specify that
257 * one or more of its PHP functions are executed at these points, using the
260 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
266 * @param string $tag The name of the action to which the $function_to_add is hooked.
267 * @param callback $function_to_add The name of the function you wish to be called.
268 * @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.
269 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
271 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
272 return add_filter($tag, $function_to_add, $priority, $accepted_args);
277 * Execute functions hooked on a specific action hook.
279 * This function invokes all functions attached to action hook $tag. It is
280 * possible to create new action hooks by simply calling this function,
281 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
283 * You can pass extra arguments to the hooks, much like you can with
286 * @see apply_filters() This function works similar with the exception that
287 * nothing is returned and only the functions or methods are called.
292 * @global array $wp_filter Stores all of the filters
293 * @global array $wp_actions Increments the amount of times action was triggered.
295 * @param string $tag The name of the action to be executed.
296 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
297 * @return null Will return null if $tag does not exist in $wp_filter array
299 function do_action($tag, $arg = '') {
300 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
302 if ( is_array($wp_actions) )
303 $wp_actions[] = $tag;
305 $wp_actions = array($tag);
307 $wp_current_filter[] = $tag;
309 // Do 'all' actions first
310 if ( isset($wp_filter['all']) ) {
311 $all_args = func_get_args();
312 _wp_call_all_hook($all_args);
315 if ( !isset($wp_filter[$tag]) ) {
316 array_pop($wp_current_filter);
321 if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
325 for ( $a = 2; $a < func_num_args(); $a++ )
326 $args[] = func_get_arg($a);
329 if ( !isset( $merged_filters[ $tag ] ) ) {
330 ksort($wp_filter[$tag]);
331 $merged_filters[ $tag ] = true;
334 reset( $wp_filter[ $tag ] );
337 foreach ( (array) current($wp_filter[$tag]) as $the_ )
338 if ( !is_null($the_['function']) )
339 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
341 } while ( next($wp_filter[$tag]) !== false );
343 array_pop($wp_current_filter);
347 * Retrieve the number times an action is fired.
352 * @global array $wp_actions Increments the amount of times action was triggered.
354 * @param string $tag The name of the action hook.
355 * @return int The number of times action hook <tt>$tag</tt> is fired
357 function did_action($tag) {
360 if ( empty($wp_actions) )
363 return count(array_keys($wp_actions, $tag));
367 * Execute functions hooked on a specific action hook, specifying arguments in an array.
369 * @see do_action() This function is identical, but the arguments passed to the
370 * functions hooked to <tt>$tag</tt> are supplied using an array.
375 * @global array $wp_filter Stores all of the filters
376 * @global array $wp_actions Increments the amount of times action was triggered.
378 * @param string $tag The name of the action to be executed.
379 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
380 * @return null Will return null if $tag does not exist in $wp_filter array
382 function do_action_ref_array($tag, $args) {
383 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
385 if ( !is_array($wp_actions) )
386 $wp_actions = array($tag);
388 $wp_actions[] = $tag;
390 $wp_current_filter[] = $tag;
392 // Do 'all' actions first
393 if ( isset($wp_filter['all']) ) {
394 $all_args = func_get_args();
395 _wp_call_all_hook($all_args);
398 if ( !isset($wp_filter[$tag]) ) {
399 array_pop($wp_current_filter);
404 if ( !isset( $merged_filters[ $tag ] ) ) {
405 ksort($wp_filter[$tag]);
406 $merged_filters[ $tag ] = true;
409 reset( $wp_filter[ $tag ] );
412 foreach( (array) current($wp_filter[$tag]) as $the_ )
413 if ( !is_null($the_['function']) )
414 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
416 } while ( next($wp_filter[$tag]) !== false );
418 array_pop($wp_current_filter);
422 * Check if any action has been registered for a hook.
427 * @see has_filter() has_action() is an alias of has_filter().
429 * @param string $tag The name of the action hook.
430 * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
431 * @return int|boolean Optionally returns the priority on that hook for the specified function.
433 function has_action($tag, $function_to_check = false) {
434 return has_filter($tag, $function_to_check);
438 * Removes a function from a specified action hook.
440 * This function removes a function attached to a specified action hook. This
441 * method can be used to remove default functions attached to a specific filter
442 * hook and possibly replace them with a substitute.
448 * @param string $tag The action hook to which the function to be removed is hooked.
449 * @param callback $function_to_remove The name of the function which should be removed.
450 * @param int $priority optional The priority of the function (default: 10).
451 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
452 * @return boolean Whether the function is removed.
454 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
455 return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
459 * Remove all of the hooks from an action.
463 * @param string $tag The action to remove hooks from.
464 * @param int $priority The priority number to remove them from.
465 * @return bool True when finished.
467 function remove_all_actions($tag, $priority = false) {
468 return remove_all_filters($tag, $priority);
472 // Functions for handling plugins.
476 * Gets the basename of a plugin.
478 * This method extracts the name of a plugin from its filename.
486 * @param string $file The filename of plugin.
487 * @return string The name of a plugin.
488 * @uses WP_PLUGIN_DIR
490 function plugin_basename($file) {
491 $file = str_replace('\\','/',$file); // sanitize for Win32 installs
492 $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
493 $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
494 $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
495 $file = preg_replace('|^' . preg_quote($plugin_dir, '|') . '/|','',$file); // get relative path from plugins dir
500 * Set the activation hook for a plugin.
502 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
503 * activated. In the name of this hook, PLUGINNAME is replaced with the name of
504 * the plugin, including the optional subdirectory. For example, when the plugin
505 * is located in wp-content/plugin/sampleplugin/sample.php, then the name of
506 * this hook will become 'activate_sampleplugin/sample.php'. When the plugin
507 * consists of only one file and is (as by default) located at
508 * wp-content/plugin/sample.php the name of this hook will be
509 * 'activate_sample.php'.
517 * @param string $file The filename of the plugin including the path.
518 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
520 function register_activation_hook($file, $function) {
521 $file = plugin_basename($file);
522 add_action('activate_' . $file, $function);
526 * Set the deactivation hook for a plugin.
528 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
529 * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
530 * of the plugin, including the optional subdirectory. For example, when the
531 * plugin is located in wp-content/plugin/sampleplugin/sample.php, then
532 * the name of this hook will become 'activate_sampleplugin/sample.php'.
534 * When the plugin consists of only one file and is (as by default) located at
535 * wp-content/plugin/sample.php the name of this hook will be
536 * 'activate_sample.php'.
544 * @param string $file The filename of the plugin including the path.
545 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
547 function register_deactivation_hook($file, $function) {
548 $file = plugin_basename($file);
549 add_action('deactivate_' . $file, $function);
553 * Set the uninstallation hook for a plugin.
555 * Registers the uninstall hook that will be called when the user clicks on the
556 * uninstall link that calls for the plugin to uninstall itself. The link won't
557 * be active unless the plugin hooks into the action.
559 * The plugin should not run arbitrary code outside of functions, when
560 * registering the uninstall hook. In order to run using the hook, the plugin
561 * will have to be included, which means that any code laying outside of a
562 * function will be run during the uninstall process. The plugin should not
563 * hinder the uninstall process.
565 * If the plugin can not be written without running code within the plugin, then
566 * the plugin should create a file named 'uninstall.php' in the base plugin
567 * folder. This file will be called, if it exists, during the uninstall process
568 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
569 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
574 * @param string $file
575 * @param callback $callback The callback to run when the hook is called.
577 function register_uninstall_hook($file, $callback) {
578 // The option should not be autoloaded, because it is not needed in most
579 // cases. Emphasis should be put on using the 'uninstall.php' way of
580 // uninstalling the plugin.
581 $uninstallable_plugins = (array) get_option('uninstall_plugins');
582 $uninstallable_plugins[plugin_basename($file)] = $callback;
583 update_option('uninstall_plugins', $uninstallable_plugins);
587 * Calls the 'all' hook, which will process the functions hooked into it.
589 * The 'all' hook passes all of the arguments or parameters that were used for
590 * the hook, which this function was called for.
592 * This function is used internally for apply_filters(), do_action(), and
593 * do_action_ref_array() and is not meant to be used from outside those
594 * functions. This function does not check for the existence of the all hook, so
595 * it will fail unless the all hook exists prior to this function call.
602 * @uses $wp_filter Used to process all of the functions in the 'all' hook
604 * @param array $args The collected parameters from the hook that was called.
605 * @param string $hook Optional. The hook name that was used to call the 'all' hook.
607 function _wp_call_all_hook($args) {
610 reset( $wp_filter['all'] );
612 foreach( (array) current($wp_filter['all']) as $the_ )
613 if ( !is_null($the_['function']) )
614 call_user_func_array($the_['function'], $args);
616 } while ( next($wp_filter['all']) !== false );
620 * Build Unique ID for storage and retrieval.
622 * The old way to serialize the callback caused issues and this function is the
623 * solution. It works by checking for objects and creating an a new property in
624 * the class to keep track of the object and new objects of the same class that
627 * It also allows for the removal of actions and filters for objects after they
628 * change class properties. It is possible to include the property $wp_filter_id
629 * in your class and set it to "null" or a number to bypass the workaround.
630 * However this will prevent you from adding new classes and any new classes
631 * will overwrite the previous hook by the same class.
633 * Functions and static method callbacks are just returned as strings and
634 * shouldn't have any speed penalty.
640 * @link http://trac.wordpress.org/ticket/3875
642 * @global array $wp_filter Storage for all of the filters and actions
643 * @param string $tag Used in counting how many hooks were applied
644 * @param string|array $function Used for creating unique id
645 * @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.
646 * @param string $type filter or action
647 * @return string Unique ID for usage as array key
649 function _wp_filter_build_unique_id($tag, $function, $priority) {
652 // If function then just skip all of the tests and not overwrite the following.
653 if ( is_string($function) )
655 // Object Class Calling
656 else if (is_object($function[0]) ) {
657 $obj_idx = get_class($function[0]).$function[1];
658 if ( !isset($function[0]->wp_filter_id) ) {
659 if ( false === $priority )
661 $count = isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : 0;
662 $function[0]->wp_filter_id = $count;
666 $obj_idx .= $function[0]->wp_filter_id;
670 else if ( is_string($function[0]) )
671 return $function[0].$function[1];