3
* The plugin API is located in this file, which allows for creating actions
4
* and filters and hooking functions, and methods. The functions or methods will
5
* then be run when the action or filter is called.
7
* The API callback examples reference functions, but can be methods of classes.
8
* To hook methods, you'll need to pass an array one of two ways.
10
* Any of the syntaxes explained in the PHP documentation for the
11
* {@link http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}
14
* Also see the {@link http://codex.wordpress.org/Plugin_API Plugin API} for
15
* more information and examples on how to use a lot of these functions.
22
// Initialize the filter globals.
23
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
25
if ( ! isset( $wp_filter ) )
28
if ( ! isset( $wp_actions ) )
29
$wp_actions = array();
31
if ( ! isset( $merged_filters ) )
32
$merged_filters = array();
34
if ( ! isset( $wp_current_filter ) )
35
$wp_current_filter = array();
38
* Hooks a function or method to a specific filter action.
40
* WordPress offers filter hooks to allow plugins to modify
41
* various types of internal data at runtime.
43
* A plugin can modify data by binding a callback to a filter hook. When the filter
44
* is later applied, each bound callback is run in order of priority, and given
45
* the opportunity to modify a value by returning a new value.
47
* The following example shows how a callback function is bound to a filter hook.
48
* Note that $example is passed to the callback, (maybe) modified, then returned:
51
* function example_callback( $example ) {
52
* // Maybe modify $example in some way
55
* add_filter( 'example_filter', 'example_callback' );
58
* Since WordPress 1.5.1, bound callbacks can take as many arguments as are
59
* passed as parameters in the corresponding apply_filters() call. The $accepted_args
60
* parameter allows for calling functions only when the number of args match.
62
* <strong>Note:</strong> the function will return true whether or not the callback
63
* is valid. It is up to you to take care. This is done for optimization purposes,
64
* so everything is as quick as possible.
69
* @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
70
* @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.
74
* @param string $tag The name of the filter to hook the $function_to_add callback to.
75
* @param callback $function_to_add The callback to be run when the filter is applied.
76
* @param int $priority (optional) The order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
78
* @param int $accepted_args (optional) The number of arguments the function accepts.
80
* @return boolean true
82
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
83
global $wp_filter, $merged_filters;
85
$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
86
$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
87
unset( $merged_filters[ $tag ] );
92
* Check if any filter has been registered for a hook.
97
* @global array $wp_filter Stores all of the filters
99
* @param string $tag The name of the filter hook.
100
* @param callback $function_to_check optional.
101
* @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
102
* When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
103
* When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
104
* (e.g.) 0, so use the === operator for testing the return value.
106
function has_filter($tag, $function_to_check = false) {
109
$has = !empty($wp_filter[$tag]);
110
if ( false === $function_to_check || false == $has )
113
if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
116
foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
117
if ( isset($wp_filter[$tag][$priority][$idx]) )
125
* Call the functions added to a filter hook.
127
* The callback functions attached to filter hook $tag are invoked by calling
128
* this function. This function can be used to create a new filter hook by
129
* simply calling this function with the name of the new hook specified using
130
* the $tag parameter.
132
* The function allows for additional arguments to be added and passed to hooks.
134
* // Our filter callback function
135
* function example_callback( $string, $arg1, $arg2 ) {
136
* // (maybe) modify $string
139
* add_filter( 'example_filter', 'example_callback', 10, 3 );
141
* // Apply the filters by calling the 'example_callback' function we
142
* // "hooked" to 'example_filter' using the add_filter() function above.
143
* // - 'example_filter' is the filter hook $tag
144
* // - 'filter me' is the value being filtered
145
* // - $arg1 and $arg2 are the additional arguments passed to the callback.
146
* $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
152
* @global array $wp_filter Stores all of the filters
153
* @global array $merged_filters Merges the filter hooks using this function.
154
* @global array $wp_current_filter stores the list of current filters with the current one last
158
* @param string $tag The name of the filter hook.
159
* @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
160
* @param mixed $var Additional variables passed to the functions hooked to <tt>$tag</tt>.
161
* @return mixed The filtered value after all hooked functions are applied to it.
163
function apply_filters( $tag, $value ) {
164
global $wp_filter, $merged_filters, $wp_current_filter;
168
// Do 'all' actions first
169
if ( isset($wp_filter['all']) ) {
170
$wp_current_filter[] = $tag;
171
$args = func_get_args();
172
_wp_call_all_hook($args);
175
if ( !isset($wp_filter[$tag]) ) {
176
if ( isset($wp_filter['all']) )
177
array_pop($wp_current_filter);
181
if ( !isset($wp_filter['all']) )
182
$wp_current_filter[] = $tag;
185
if ( !isset( $merged_filters[ $tag ] ) ) {
186
ksort($wp_filter[$tag]);
187
$merged_filters[ $tag ] = true;
190
reset( $wp_filter[ $tag ] );
193
$args = func_get_args();
196
foreach( (array) current($wp_filter[$tag]) as $the_ )
197
if ( !is_null($the_['function']) ){
199
$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
202
} while ( next($wp_filter[$tag]) !== false );
204
array_pop( $wp_current_filter );
210
* Execute functions hooked on a specific filter hook, specifying arguments in an array.
212
* @see apply_filters() This function is identical, but the arguments passed to the
213
* functions hooked to <tt>$tag</tt> are supplied using an array.
218
* @global array $wp_filter Stores all of the filters
219
* @global array $merged_filters Merges the filter hooks using this function.
220
* @global array $wp_current_filter stores the list of current filters with the current one last
222
* @param string $tag The name of the filter hook.
223
* @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
224
* @return mixed The filtered value after all hooked functions are applied to it.
226
function apply_filters_ref_array($tag, $args) {
227
global $wp_filter, $merged_filters, $wp_current_filter;
229
// Do 'all' actions first
230
if ( isset($wp_filter['all']) ) {
231
$wp_current_filter[] = $tag;
232
$all_args = func_get_args();
233
_wp_call_all_hook($all_args);
236
if ( !isset($wp_filter[$tag]) ) {
237
if ( isset($wp_filter['all']) )
238
array_pop($wp_current_filter);
242
if ( !isset($wp_filter['all']) )
243
$wp_current_filter[] = $tag;
246
if ( !isset( $merged_filters[ $tag ] ) ) {
247
ksort($wp_filter[$tag]);
248
$merged_filters[ $tag ] = true;
251
reset( $wp_filter[ $tag ] );
254
foreach( (array) current($wp_filter[$tag]) as $the_ )
255
if ( !is_null($the_['function']) )
256
$args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
258
} while ( next($wp_filter[$tag]) !== false );
260
array_pop( $wp_current_filter );
266
* Removes a function from a specified filter hook.
268
* This function removes a function attached to a specified filter hook. This
269
* method can be used to remove default functions attached to a specific filter
270
* hook and possibly replace them with a substitute.
272
* To remove a hook, the $function_to_remove and $priority arguments must match
273
* when the hook was added. This goes for both filters and actions. No warning
274
* will be given on removal failure.
280
* @param string $tag The filter hook to which the function to be removed is hooked.
281
* @param callback $function_to_remove The name of the function which should be removed.
282
* @param int $priority optional. The priority of the function (default: 10).
283
* @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
284
* @return boolean Whether the function existed before it was removed.
286
function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
287
$function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
289
$r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
292
unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
293
if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
294
unset($GLOBALS['wp_filter'][$tag][$priority]);
295
unset($GLOBALS['merged_filters'][$tag]);
302
* Remove all of the hooks from a filter.
306
* @param string $tag The filter to remove hooks from.
307
* @param int $priority The priority number to remove.
308
* @return bool True when finished.
310
function remove_all_filters($tag, $priority = false) {
311
global $wp_filter, $merged_filters;
313
if( isset($wp_filter[$tag]) ) {
314
if( false !== $priority && isset($wp_filter[$tag][$priority]) )
315
unset($wp_filter[$tag][$priority]);
317
unset($wp_filter[$tag]);
320
if( isset($merged_filters[$tag]) )
321
unset($merged_filters[$tag]);
327
* Retrieve the name of the current filter or action.
333
* @return string Hook name of the current filter or action.
335
function current_filter() {
336
global $wp_current_filter;
337
return end( $wp_current_filter );
341
* Hooks a function on to a specific action.
343
* Actions are the hooks that the WordPress core launches at specific points
344
* during execution, or when specific events occur. Plugins can specify that
345
* one or more of its PHP functions are executed at these points, using the
348
* @uses add_filter() Adds an action. Parameter list and functionality are the same.
354
* @param string $tag The name of the action to which the $function_to_add is hooked.
355
* @param callback $function_to_add The name of the function you wish to be called.
356
* @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.
357
* @param int $accepted_args optional. The number of arguments the function accept (default 1).
359
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
360
return add_filter($tag, $function_to_add, $priority, $accepted_args);
364
* Execute functions hooked on a specific action hook.
366
* This function invokes all functions attached to action hook $tag. It is
367
* possible to create new action hooks by simply calling this function,
368
* specifying the name of the new hook using the <tt>$tag</tt> parameter.
370
* You can pass extra arguments to the hooks, much like you can with
373
* @see apply_filters() This function works similar with the exception that
374
* nothing is returned and only the functions or methods are called.
379
* @global array $wp_filter Stores all of the filters
380
* @global array $wp_actions Increments the amount of times action was triggered.
382
* @param string $tag The name of the action to be executed.
383
* @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
384
* @return null Will return null if $tag does not exist in $wp_filter array
386
function do_action($tag, $arg = '') {
387
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
389
if ( ! isset($wp_actions[$tag]) )
390
$wp_actions[$tag] = 1;
394
// Do 'all' actions first
395
if ( isset($wp_filter['all']) ) {
396
$wp_current_filter[] = $tag;
397
$all_args = func_get_args();
398
_wp_call_all_hook($all_args);
401
if ( !isset($wp_filter[$tag]) ) {
402
if ( isset($wp_filter['all']) )
403
array_pop($wp_current_filter);
407
if ( !isset($wp_filter['all']) )
408
$wp_current_filter[] = $tag;
411
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
415
for ( $a = 2; $a < func_num_args(); $a++ )
416
$args[] = func_get_arg($a);
419
if ( !isset( $merged_filters[ $tag ] ) ) {
420
ksort($wp_filter[$tag]);
421
$merged_filters[ $tag ] = true;
424
reset( $wp_filter[ $tag ] );
427
foreach ( (array) current($wp_filter[$tag]) as $the_ )
428
if ( !is_null($the_['function']) )
429
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
431
} while ( next($wp_filter[$tag]) !== false );
433
array_pop($wp_current_filter);
437
* Retrieve the number of times an action is fired.
442
* @global array $wp_actions Increments the amount of times action was triggered.
444
* @param string $tag The name of the action hook.
445
* @return int The number of times action hook <tt>$tag</tt> is fired
447
function did_action($tag) {
450
if ( ! isset( $wp_actions[ $tag ] ) )
453
return $wp_actions[$tag];
457
* Execute functions hooked on a specific action hook, specifying arguments in an array.
459
* @see do_action() This function is identical, but the arguments passed to the
460
* functions hooked to <tt>$tag</tt> are supplied using an array.
465
* @global array $wp_filter Stores all of the filters
466
* @global array $wp_actions Increments the amount of times action was triggered.
468
* @param string $tag The name of the action to be executed.
469
* @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
470
* @return null Will return null if $tag does not exist in $wp_filter array
472
function do_action_ref_array($tag, $args) {
473
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
475
if ( ! isset($wp_actions[$tag]) )
476
$wp_actions[$tag] = 1;
480
// Do 'all' actions first
481
if ( isset($wp_filter['all']) ) {
482
$wp_current_filter[] = $tag;
483
$all_args = func_get_args();
484
_wp_call_all_hook($all_args);
487
if ( !isset($wp_filter[$tag]) ) {
488
if ( isset($wp_filter['all']) )
489
array_pop($wp_current_filter);
493
if ( !isset($wp_filter['all']) )
494
$wp_current_filter[] = $tag;
497
if ( !isset( $merged_filters[ $tag ] ) ) {
498
ksort($wp_filter[$tag]);
499
$merged_filters[ $tag ] = true;
502
reset( $wp_filter[ $tag ] );
505
foreach( (array) current($wp_filter[$tag]) as $the_ )
506
if ( !is_null($the_['function']) )
507
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
509
} while ( next($wp_filter[$tag]) !== false );
511
array_pop($wp_current_filter);
515
* Check if any action has been registered for a hook.
520
* @see has_filter() has_action() is an alias of has_filter().
522
* @param string $tag The name of the action hook.
523
* @param callback $function_to_check optional.
524
* @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
525
* When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
526
* When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
527
* (e.g.) 0, so use the === operator for testing the return value.
529
function has_action($tag, $function_to_check = false) {
530
return has_filter($tag, $function_to_check);
534
* Removes a function from a specified action hook.
536
* This function removes a function attached to a specified action hook. This
537
* method can be used to remove default functions attached to a specific filter
538
* hook and possibly replace them with a substitute.
544
* @param string $tag The action hook to which the function to be removed is hooked.
545
* @param callback $function_to_remove The name of the function which should be removed.
546
* @param int $priority optional The priority of the function (default: 10).
547
* @return boolean Whether the function is removed.
549
function remove_action( $tag, $function_to_remove, $priority = 10 ) {
550
return remove_filter( $tag, $function_to_remove, $priority );
554
* Remove all of the hooks from an action.
558
* @param string $tag The action to remove hooks from.
559
* @param int $priority The priority number to remove them from.
560
* @return bool True when finished.
562
function remove_all_actions($tag, $priority = false) {
563
return remove_all_filters($tag, $priority);
567
// Functions for handling plugins.
571
* Gets the basename of a plugin.
573
* This method extracts the name of a plugin from its filename.
581
* @param string $file The filename of plugin.
582
* @return string The name of a plugin.
583
* @uses WP_PLUGIN_DIR
585
function plugin_basename($file) {
586
$file = str_replace('\\','/',$file); // sanitize for Win32 installs
587
$file = preg_replace('|/+|','/', $file); // remove any duplicate slash
588
$plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
589
$plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
590
$mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
591
$mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
592
$file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
593
$file = trim($file, '/');
598
* Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
603
* @param string $file The filename of the plugin (__FILE__)
604
* @return string the filesystem path of the directory that contains the plugin
606
function plugin_dir_path( $file ) {
607
return trailingslashit( dirname( $file ) );
611
* Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
616
* @param string $file The filename of the plugin (__FILE__)
617
* @return string the URL path of the directory that contains the plugin
619
function plugin_dir_url( $file ) {
620
return trailingslashit( plugins_url( '', $file ) );
624
* Set the activation hook for a plugin.
626
* When a plugin is activated, the action 'activate_PLUGINNAME' hook is
627
* called. In the name of this hook, PLUGINNAME is replaced with the name
628
* of the plugin, including the optional subdirectory. For example, when the
629
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then
630
* the name of this hook will become 'activate_sampleplugin/sample.php'.
632
* When the plugin consists of only one file and is (as by default) located at
633
* wp-content/plugins/sample.php the name of this hook will be
634
* 'activate_sample.php'.
640
* @param string $file The filename of the plugin including the path.
641
* @param callback $function the function hooked to the 'activate_PLUGIN' action.
643
function register_activation_hook($file, $function) {
644
$file = plugin_basename($file);
645
add_action('activate_' . $file, $function);
649
* Set the deactivation hook for a plugin.
651
* When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
652
* called. In the name of this hook, PLUGINNAME is replaced with the name
653
* of the plugin, including the optional subdirectory. For example, when the
654
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then
655
* the name of this hook will become 'deactivate_sampleplugin/sample.php'.
657
* When the plugin consists of only one file and is (as by default) located at
658
* wp-content/plugins/sample.php the name of this hook will be
659
* 'deactivate_sample.php'.
665
* @param string $file The filename of the plugin including the path.
666
* @param callback $function the function hooked to the 'deactivate_PLUGIN' action.
668
function register_deactivation_hook($file, $function) {
669
$file = plugin_basename($file);
670
add_action('deactivate_' . $file, $function);
674
* Set the uninstallation hook for a plugin.
676
* Registers the uninstall hook that will be called when the user clicks on the
677
* uninstall link that calls for the plugin to uninstall itself. The link won't
678
* be active unless the plugin hooks into the action.
680
* The plugin should not run arbitrary code outside of functions, when
681
* registering the uninstall hook. In order to run using the hook, the plugin
682
* will have to be included, which means that any code laying outside of a
683
* function will be run during the uninstall process. The plugin should not
684
* hinder the uninstall process.
686
* If the plugin can not be written without running code within the plugin, then
687
* the plugin should create a file named 'uninstall.php' in the base plugin
688
* folder. This file will be called, if it exists, during the uninstall process
689
* bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
690
* should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
695
* @param string $file
696
* @param callback $callback The callback to run when the hook is called. Must be a static method or function.
698
function register_uninstall_hook( $file, $callback ) {
699
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
700
_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
704
// The option should not be autoloaded, because it is not needed in most
705
// cases. Emphasis should be put on using the 'uninstall.php' way of
706
// uninstalling the plugin.
707
$uninstallable_plugins = (array) get_option('uninstall_plugins');
708
$uninstallable_plugins[plugin_basename($file)] = $callback;
709
update_option('uninstall_plugins', $uninstallable_plugins);
713
* Calls the 'all' hook, which will process the functions hooked into it.
715
* The 'all' hook passes all of the arguments or parameters that were used for
716
* the hook, which this function was called for.
718
* This function is used internally for apply_filters(), do_action(), and
719
* do_action_ref_array() and is not meant to be used from outside those
720
* functions. This function does not check for the existence of the all hook, so
721
* it will fail unless the all hook exists prior to this function call.
728
* @uses $wp_filter Used to process all of the functions in the 'all' hook
730
* @param array $args The collected parameters from the hook that was called.
732
function _wp_call_all_hook($args) {
735
reset( $wp_filter['all'] );
737
foreach( (array) current($wp_filter['all']) as $the_ )
738
if ( !is_null($the_['function']) )
739
call_user_func_array($the_['function'], $args);
741
} while ( next($wp_filter['all']) !== false );
745
* Build Unique ID for storage and retrieval.
747
* The old way to serialize the callback caused issues and this function is the
748
* solution. It works by checking for objects and creating an a new property in
749
* the class to keep track of the object and new objects of the same class that
752
* It also allows for the removal of actions and filters for objects after they
753
* change class properties. It is possible to include the property $wp_filter_id
754
* in your class and set it to "null" or a number to bypass the workaround.
755
* However this will prevent you from adding new classes and any new classes
756
* will overwrite the previous hook by the same class.
758
* Functions and static method callbacks are just returned as strings and
759
* shouldn't have any speed penalty.
765
* @link http://trac.wordpress.org/ticket/3875
767
* @global array $wp_filter Storage for all of the filters and actions
768
* @param string $tag Used in counting how many hooks were applied
769
* @param callback $function Used for creating unique id
770
* @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.
771
* @return string|bool Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a unique id.
773
function _wp_filter_build_unique_id($tag, $function, $priority) {
775
static $filter_id_count = 0;
777
if ( is_string($function) )
780
if ( is_object($function) ) {
781
// Closures are currently implemented as objects
782
$function = array( $function, '' );
784
$function = (array) $function;
787
if (is_object($function[0]) ) {
788
// Object Class Calling
789
if ( function_exists('spl_object_hash') ) {
790
return spl_object_hash($function[0]) . $function[1];
792
$obj_idx = get_class($function[0]).$function[1];
793
if ( !isset($function[0]->wp_filter_id) ) {
794
if ( false === $priority )
796
$obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
797
$function[0]->wp_filter_id = $filter_id_count;
800
$obj_idx .= $function[0]->wp_filter_id;
805
} else if ( is_string($function[0]) ) {
807
return $function[0] . '::' . $function[1];