| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 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. 6 * 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. 9 * 10 * Any of the syntaxes explained in the PHP documentation for the 11 * {@link https://www.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'} 12 * type are valid. 13 * 14 * Also see the {@link https://developer.wordpress.org/plugins/ Plugin API} for 15 * more information and examples on how to use a lot of these functions. 16 * 17 * This file should have no external dependencies. 18 * 19 * @package WordPress 20 * @subpackage Plugin 21 * @since 1.5.0 22 */ 23 24 // Initialize the filter globals. 25 require __DIR__ . '/class-wp-hook.php'; 26 require __DIR__ . '/class-wp-filter-sentinel.php'; 27 28 /** @var WP_Hook[] $wp_filter */ 29 global $wp_filter; 30 31 /** @var int[] $wp_actions */ 32 global $wp_actions; 33 34 /** @var int[] $wp_filters */ 35 global $wp_filters; 36 37 /** @var string[] $wp_current_filter */ 38 global $wp_current_filter; 39 40 if ( $wp_filter ) { 41 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); 42 } else { 43 $wp_filter = array(); 44 } 45 46 if ( ! isset( $wp_actions ) ) { 47 $wp_actions = array(); 48 } 49 50 if ( ! isset( $wp_filters ) ) { 51 $wp_filters = array(); 52 } 53 54 if ( ! isset( $wp_current_filter ) ) { 55 $wp_current_filter = array(); 56 } 57 58 /** 59 * Adds a callback function to a filter hook. 60 * 61 * WordPress offers filter hooks to allow plugins to modify 62 * various types of internal data at runtime. 63 * 64 * A plugin can modify data by binding a callback to a filter hook. When the filter 65 * is later applied, each bound callback is run in order of priority, and given 66 * the opportunity to modify a value by returning a new value. 67 * 68 * The following example shows how a callback function is bound to a filter hook. 69 * 70 * Note that `$example` is passed to the callback, (maybe) modified, then returned: 71 * 72 * function example_callback( $example ) { 73 * // Maybe modify $example in some way. 74 * return $example; 75 * } 76 * add_filter( 'example_filter', 'example_callback' ); 77 * 78 * Bound callbacks can accept from none to the total number of arguments passed as parameters 79 * in the corresponding apply_filters() call. 80 * 81 * In other words, if an apply_filters() call passes four total arguments, callbacks bound to 82 * it can accept none (the same as 1) of the arguments or up to four. The important part is that 83 * the `$accepted_args` value must reflect the number of arguments the bound callback *actually* 84 * opted to accept. If no arguments were accepted by the callback that is considered to be the 85 * same as accepting 1 argument. For example: 86 * 87 * // Filter call. 88 * $value = apply_filters( 'hook', $value, $arg2, $arg3 ); 89 * 90 * // Accepting zero/one arguments. 91 * function example_callback() { 92 * ... 93 * return 'some value'; 94 * } 95 * add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1. 96 * 97 * // Accepting two arguments (three possible). 98 * function example_callback( $value, $arg2 ) { 99 * ... 100 * return $maybe_modified_value; 101 * } 102 * add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2. 103 * 104 * *Note:* The function will return true whether or not the callback is valid. 105 * It is up to you to take care. This is done for optimization purposes, so 106 * everything is as quick as possible. 107 * 108 * @since 0.71 109 * 110 * @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. 111 * 112 * @param string $hook_name The name of the filter to add the callback to. 113 * @param callable $callback The callback to be run when the filter is applied. 114 * @param int $priority Optional. Used to specify the order in which the functions 115 * associated with a particular filter are executed. 116 * Lower numbers correspond with earlier execution, 117 * and functions with the same priority are executed 118 * in the order in which they were added to the filter. Default 10. 119 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. 120 * @return true Always returns true. 121 */ 122 function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { 123 global $wp_filter; 124 125 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 126 $wp_filter[ $hook_name ] = new WP_Hook(); 127 } 128 129 $wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args ); 130 131 return true; 132 } 133 134 /** 135 * Calls the callback functions that have been added to a filter hook. 136 * 137 * This function invokes all functions attached to filter hook `$hook_name`. 138 * It is possible to create new filter hooks by simply calling this function, 139 * specifying the name of the new hook using the `$hook_name` parameter. 140 * 141 * The function also allows for multiple additional arguments to be passed to hooks. 142 * 143 * Example usage: 144 * 145 * // The filter callback function. 146 * function example_callback( $string, $arg1, $arg2 ) { 147 * // (maybe) modify $string. 148 * return $string; 149 * } 150 * add_filter( 'example_filter', 'example_callback', 10, 3 ); 151 * 152 * /* 153 * * Apply the filters by calling the 'example_callback()' function 154 * * that's hooked onto `example_filter` above. 155 * * 156 * * - 'example_filter' is the filter hook. 157 * * - 'filter me' is the value being filtered. 158 * * - $arg1 and $arg2 are the additional arguments passed to the callback. 159 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); 160 * 161 * @since 0.71 162 * @since 6.0.0 Formalized the existing and already documented `...$args` parameter 163 * by adding it to the function signature. 164 * 165 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 166 * @global int[] $wp_filters Stores the number of times each filter was triggered. 167 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 168 * 169 * @param string $hook_name The name of the filter hook. 170 * @param mixed $value The value to filter. 171 * @param mixed ...$args Optional. Additional parameters to pass to the callback functions. 172 * @no-named-arguments 173 * @return mixed The filtered value after all hooked functions are applied to it. 174 */ 175 function apply_filters( $hook_name, $value, ...$args ) { 176 global $wp_filter, $wp_filters, $wp_current_filter; 177 178 if ( ! isset( $wp_filters[ $hook_name ] ) ) { 179 $wp_filters[ $hook_name ] = 1; 180 } else { 181 ++$wp_filters[ $hook_name ]; 182 } 183 184 // Do 'all' actions first. 185 if ( isset( $wp_filter['all'] ) ) { 186 $wp_current_filter[] = $hook_name; 187 188 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 189 _wp_call_all_hook( $all_args ); 190 } 191 192 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 193 if ( isset( $wp_filter['all'] ) ) { 194 array_pop( $wp_current_filter ); 195 } 196 197 return $value; 198 } 199 200 if ( ! isset( $wp_filter['all'] ) ) { 201 $wp_current_filter[] = $hook_name; 202 } 203 204 // Pass the value to WP_Hook. 205 array_unshift( $args, $value ); 206 207 $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); 208 209 array_pop( $wp_current_filter ); 210 211 return $filtered; 212 } 213 214 /** 215 * Calls the callback functions that have been added to a filter hook, specifying arguments in an array. 216 * 217 * @since 3.0.0 218 * 219 * @see apply_filters() This function is identical, but the arguments passed to the 220 * functions hooked to `$hook_name` are supplied using an array. 221 * 222 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 223 * @global int[] $wp_filters Stores the number of times each filter was triggered. 224 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 225 * 226 * @param string $hook_name The name of the filter hook. 227 * @param non-empty-list<mixed> $args The arguments supplied to the functions hooked to `$hook_name`. 228 * @return mixed The filtered value after all hooked functions are applied to it. 229 */ 230 function apply_filters_ref_array( $hook_name, $args ) { 231 global $wp_filter, $wp_filters, $wp_current_filter; 232 233 if ( ! isset( $wp_filters[ $hook_name ] ) ) { 234 $wp_filters[ $hook_name ] = 1; 235 } else { 236 ++$wp_filters[ $hook_name ]; 237 } 238 239 // Do 'all' actions first. 240 if ( isset( $wp_filter['all'] ) ) { 241 $wp_current_filter[] = $hook_name; 242 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 243 _wp_call_all_hook( $all_args ); 244 } 245 246 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 247 if ( isset( $wp_filter['all'] ) ) { 248 array_pop( $wp_current_filter ); 249 } 250 251 return $args[0]; 252 } 253 254 if ( ! isset( $wp_filter['all'] ) ) { 255 $wp_current_filter[] = $hook_name; 256 } 257 258 $filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args ); 259 260 array_pop( $wp_current_filter ); 261 262 return $filtered; 263 } 264 265 /** 266 * Checks if any filter has been registered for a hook. 267 * 268 * When using the `$callback` argument, this function may return a non-boolean value 269 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 270 * 271 * @since 2.5.0 272 * @since 6.9.0 Added the `$priority` parameter. 273 * 274 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 275 * 276 * @param string $hook_name The name of the filter hook. 277 * @param callable|string|array|false $callback Optional. The callback to check for. 278 * This function can be called unconditionally to speculatively check 279 * a callback that may or may not exist. Default false. 280 * @param int|false $priority Optional. The specific priority at which to check for the callback. 281 * Default false. 282 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 283 * anything registered. When checking a specific function, the priority 284 * of that hook is returned, or false if the function is not attached. 285 * If `$callback` and `$priority` are both provided, a boolean is returned 286 * for whether the specific function is registered at that priority. 287 * @phpstan-param Maybe_Callable|false $callback 288 */ 289 function has_filter( $hook_name, $callback = false, $priority = false ) { 290 global $wp_filter; 291 292 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 293 return false; 294 } 295 296 return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback, $priority ); 297 } 298 299 /** 300 * Removes a callback function from a filter hook. 301 * 302 * This can be used to remove default functions attached to a specific filter 303 * hook and possibly replace them with a substitute. 304 * 305 * To remove a hook, the `$callback` and `$priority` arguments must match 306 * when the hook was added. This goes for both filters and actions. No warning 307 * will be given on removal failure. 308 * 309 * @since 1.2.0 310 * 311 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 312 * 313 * @param string $hook_name The filter hook to which the function to be removed is hooked. 314 * @param callable|string|array $callback The callback to be removed from running when the filter is applied. 315 * This function can be called unconditionally to speculatively remove 316 * a callback that may or may not exist. 317 * @param int $priority Optional. The exact priority used when adding the original 318 * filter callback. Default 10. 319 * @return bool Whether the function existed before it was removed. 320 * @phpstan-param Maybe_Callable $callback 321 */ 322 function remove_filter( $hook_name, $callback, $priority = 10 ) { 323 global $wp_filter; 324 325 $r = false; 326 327 if ( isset( $wp_filter[ $hook_name ] ) ) { 328 $r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority ); 329 330 if ( ! $wp_filter[ $hook_name ]->callbacks ) { 331 unset( $wp_filter[ $hook_name ] ); 332 } 333 } 334 335 return $r; 336 } 337 338 /** 339 * Removes all of the callback functions from a filter hook. 340 * 341 * @since 2.7.0 342 * 343 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 344 * 345 * @param string $hook_name The filter to remove callbacks from. 346 * @param int|false $priority Optional. The priority number to remove them from. 347 * Default false. 348 * @return true Always returns true. 349 */ 350 function remove_all_filters( $hook_name, $priority = false ) { 351 global $wp_filter; 352 353 if ( isset( $wp_filter[ $hook_name ] ) ) { 354 $wp_filter[ $hook_name ]->remove_all_filters( $priority ); 355 356 if ( ! $wp_filter[ $hook_name ]->has_filters() ) { 357 unset( $wp_filter[ $hook_name ] ); 358 } 359 } 360 361 return true; 362 } 363 364 /** 365 * Retrieves the name of the current filter hook. 366 * 367 * @since 2.5.0 368 * 369 * @global string[] $wp_current_filter Stores the list of current filters with the current one last 370 * 371 * @return string|false Hook name of the current filter, false if no filter is running. 372 */ 373 function current_filter() { 374 global $wp_current_filter; 375 376 return end( $wp_current_filter ); 377 } 378 379 /** 380 * Returns whether or not a filter hook is currently being processed. 381 * 382 * The function current_filter() only returns the most recent filter being executed. 383 * did_filter() returns the number of times a filter has been applied during 384 * the current request. 385 * 386 * This function allows detection for any filter currently being executed 387 * (regardless of whether it's the most recent filter to fire, in the case of 388 * hooks called from hook callbacks) to be verified. 389 * 390 * @since 3.9.0 391 * 392 * @see current_filter() 393 * @see did_filter() 394 * @global string[] $wp_current_filter Current filter. 395 * 396 * @param string|null $hook_name Optional. Filter hook to check. Defaults to null, 397 * which checks if any filter is currently being run. 398 * @return bool Whether the filter is currently in the stack. 399 */ 400 function doing_filter( $hook_name = null ) { 401 global $wp_current_filter; 402 403 if ( null === $hook_name ) { 404 return ! empty( $wp_current_filter ); 405 } 406 407 return in_array( $hook_name, $wp_current_filter, true ); 408 } 409 410 /** 411 * Retrieves the number of times a filter has been applied during the current request. 412 * 413 * @since 6.1.0 414 * 415 * @global int[] $wp_filters Stores the number of times each filter was triggered. 416 * 417 * @param string $hook_name The name of the filter hook. 418 * @return int The number of times the filter hook has been applied. 419 */ 420 function did_filter( $hook_name ) { 421 global $wp_filters; 422 423 if ( ! isset( $wp_filters[ $hook_name ] ) ) { 424 return 0; 425 } 426 427 return $wp_filters[ $hook_name ]; 428 } 429 430 /** 431 * Adds a callback function to an action hook. 432 * 433 * Actions are the hooks that the WordPress core launches at specific points 434 * during execution, or when specific events occur. Plugins can specify that 435 * one or more of its PHP functions are executed at these points, using the 436 * Action API. 437 * 438 * @since 1.2.0 439 * 440 * @param string $hook_name The name of the action to add the callback to. 441 * @param callable $callback The callback to be run when the action is called. 442 * @param int $priority Optional. Used to specify the order in which the functions 443 * associated with a particular action are executed. 444 * Lower numbers correspond with earlier execution, 445 * and functions with the same priority are executed 446 * in the order in which they were added to the action. Default 10. 447 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. 448 * @return true Always returns true. 449 */ 450 function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { 451 return add_filter( $hook_name, $callback, $priority, $accepted_args ); 452 } 453 454 /** 455 * Calls the callback functions that have been added to an action hook. 456 * 457 * This function invokes all functions attached to action hook `$hook_name`. 458 * It is possible to create new action hooks by simply calling this function, 459 * specifying the name of the new hook using the `$hook_name` parameter. 460 * 461 * You can pass extra arguments to the hooks, much like you can with `apply_filters()`. 462 * 463 * Example usage: 464 * 465 * // The action callback function. 466 * function example_callback( $arg1, $arg2 ) { 467 * // (maybe) do something with the args. 468 * } 469 * add_action( 'example_action', 'example_callback', 10, 2 ); 470 * 471 * /* 472 * * Trigger the actions by calling the 'example_callback()' function 473 * * that's hooked onto `example_action` above. 474 * * 475 * * - 'example_action' is the action hook. 476 * * - $arg1 and $arg2 are the additional arguments passed to the callback. 477 * do_action( 'example_action', $arg1, $arg2 ); 478 * 479 * @since 1.2.0 480 * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter 481 * by adding it to the function signature. 482 * 483 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 484 * @global int[] $wp_actions Stores the number of times each action was triggered. 485 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 486 * 487 * @param string $hook_name The name of the action to be executed. 488 * @param mixed ...$arg Optional. Additional arguments which are passed on to the 489 * functions hooked to the action. Default empty. 490 * @no-named-arguments 491 */ 492 function do_action( $hook_name, ...$arg ) { 493 global $wp_filter, $wp_actions, $wp_current_filter; 494 495 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 496 $wp_actions[ $hook_name ] = 1; 497 } else { 498 ++$wp_actions[ $hook_name ]; 499 } 500 501 // Do 'all' actions first. 502 if ( isset( $wp_filter['all'] ) ) { 503 $wp_current_filter[] = $hook_name; 504 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 505 _wp_call_all_hook( $all_args ); 506 } 507 508 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 509 if ( isset( $wp_filter['all'] ) ) { 510 array_pop( $wp_current_filter ); 511 } 512 513 return; 514 } 515 516 if ( ! isset( $wp_filter['all'] ) ) { 517 $wp_current_filter[] = $hook_name; 518 } 519 520 if ( empty( $arg ) ) { 521 $arg[] = ''; 522 } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) { 523 // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`. 524 $arg[0] = $arg[0][0]; 525 } 526 527 $wp_filter[ $hook_name ]->do_action( $arg ); 528 529 array_pop( $wp_current_filter ); 530 } 531 532 /** 533 * Calls the callback functions that have been added to an action hook, specifying arguments in an array. 534 * 535 * @since 2.1.0 536 * 537 * @see do_action() This function is identical, but the arguments passed to the 538 * functions hooked to `$hook_name` are supplied using an array. 539 * 540 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 541 * @global int[] $wp_actions Stores the number of times each action was triggered. 542 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 543 * 544 * @param string $hook_name The name of the action to be executed. 545 * @param list<mixed> $args The arguments supplied to the functions hooked to `$hook_name`. 546 */ 547 function do_action_ref_array( $hook_name, $args ) { 548 global $wp_filter, $wp_actions, $wp_current_filter; 549 550 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 551 $wp_actions[ $hook_name ] = 1; 552 } else { 553 ++$wp_actions[ $hook_name ]; 554 } 555 556 // Do 'all' actions first. 557 if ( isset( $wp_filter['all'] ) ) { 558 $wp_current_filter[] = $hook_name; 559 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 560 _wp_call_all_hook( $all_args ); 561 } 562 563 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 564 if ( isset( $wp_filter['all'] ) ) { 565 array_pop( $wp_current_filter ); 566 } 567 568 return; 569 } 570 571 if ( ! isset( $wp_filter['all'] ) ) { 572 $wp_current_filter[] = $hook_name; 573 } 574 575 $wp_filter[ $hook_name ]->do_action( $args ); 576 577 array_pop( $wp_current_filter ); 578 } 579 580 /** 581 * Checks if any action has been registered for a hook. 582 * 583 * When using the `$callback` argument, this function may return a non-boolean value 584 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 585 * 586 * @since 2.5.0 587 * @since 6.9.0 Added the `$priority` parameter. 588 * 589 * @see has_filter() This function is an alias of has_filter(). 590 * 591 * @param string $hook_name The name of the action hook. 592 * @param callable|string|array|false $callback Optional. The callback to check for. 593 * This function can be called unconditionally to speculatively check 594 * a callback that may or may not exist. Default false. 595 * @param int|false $priority Optional. The specific priority at which to check for the callback. 596 * Default false. 597 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 598 * anything registered. When checking a specific function, the priority 599 * of that hook is returned, or false if the function is not attached. 600 * If `$callback` and `$priority` are both provided, a boolean is returned 601 * for whether the specific function is registered at that priority. 602 * @phpstan-param Maybe_Callable|false $callback 603 */ 604 function has_action( $hook_name, $callback = false, $priority = false ) { 605 return has_filter( $hook_name, $callback, $priority ); 606 } 607 608 /** 609 * Removes a callback function from an action hook. 610 * 611 * This can be used to remove default functions attached to a specific action 612 * hook and possibly replace them with a substitute. 613 * 614 * To remove a hook, the `$callback` and `$priority` arguments must match 615 * when the hook was added. This goes for both filters and actions. No warning 616 * will be given on removal failure. 617 * 618 * @since 1.2.0 619 * 620 * @param string $hook_name The action hook to which the function to be removed is hooked. 621 * @param callable|string|array $callback The name of the function which should be removed. 622 * This function can be called unconditionally to speculatively remove 623 * a callback that may or may not exist. 624 * @param int $priority Optional. The exact priority used when adding the original 625 * action callback. Default 10. 626 * @return bool Whether the function is removed. 627 * @phpstan-param Maybe_Callable $callback 628 */ 629 function remove_action( $hook_name, $callback, $priority = 10 ) { 630 return remove_filter( $hook_name, $callback, $priority ); 631 } 632 633 /** 634 * Removes all of the callback functions from an action hook. 635 * 636 * @since 2.7.0 637 * 638 * @param string $hook_name The action to remove callbacks from. 639 * @param int|false $priority Optional. The priority number to remove them from. 640 * Default false. 641 * @return true Always returns true. 642 */ 643 function remove_all_actions( $hook_name, $priority = false ) { 644 return remove_all_filters( $hook_name, $priority ); 645 } 646 647 /** 648 * Retrieves the name of the current action hook. 649 * 650 * @since 3.9.0 651 * 652 * @return string|false Hook name of the current action, false if no action is running. 653 */ 654 function current_action() { 655 return current_filter(); 656 } 657 658 /** 659 * Returns whether or not an action hook is currently being processed. 660 * 661 * The function current_action() only returns the most recent action being executed. 662 * did_action() returns the number of times an action has been fired during 663 * the current request. 664 * 665 * This function allows detection for any action currently being executed 666 * (regardless of whether it's the most recent action to fire, in the case of 667 * hooks called from hook callbacks) to be verified. 668 * 669 * @since 3.9.0 670 * 671 * @see current_action() 672 * @see did_action() 673 * 674 * @param string|null $hook_name Optional. Action hook to check. Defaults to null, 675 * which checks if any action is currently being run. 676 * @return bool Whether the action is currently in the stack. 677 */ 678 function doing_action( $hook_name = null ) { 679 return doing_filter( $hook_name ); 680 } 681 682 /** 683 * Retrieves the number of times an action has been fired during the current request. 684 * 685 * @since 2.1.0 686 * 687 * @global int[] $wp_actions Stores the number of times each action was triggered. 688 * 689 * @param string $hook_name The name of the action hook. 690 * @return int The number of times the action hook has been fired. 691 */ 692 function did_action( $hook_name ) { 693 global $wp_actions; 694 695 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 696 return 0; 697 } 698 699 return $wp_actions[ $hook_name ]; 700 } 701 702 /** 703 * Fires functions attached to a deprecated filter hook. 704 * 705 * When a filter hook is deprecated, the apply_filters() call is replaced with 706 * apply_filters_deprecated(), which triggers a deprecation notice and then fires 707 * the original filter hook. 708 * 709 * Note: the value and extra arguments passed to the original apply_filters() call 710 * must be passed here to `$args` as an array. For example: 711 * 712 * // Old filter. 713 * return apply_filters( 'wpdocs_filter', $value, $extra_arg ); 714 * 715 * // Deprecated. 716 * return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9.0', 'wpdocs_new_filter' ); 717 * 718 * @since 4.6.0 719 * 720 * @see _deprecated_hook() 721 * 722 * @param string $hook_name The name of the filter hook. 723 * @param non-empty-list<mixed> $args Array of additional function arguments to be passed to apply_filters(). 724 * @param string $version The version of WordPress that deprecated the hook. 725 * @param string $replacement Optional. The hook that should have been used. Default empty. 726 * @param string $message Optional. A message regarding the change. Default empty. 727 * @return mixed The filtered value after all hooked functions are applied to it. 728 */ 729 function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { 730 if ( ! has_filter( $hook_name ) ) { 731 return $args[0]; 732 } 733 734 _deprecated_hook( $hook_name, $version, $replacement, $message ); 735 736 return apply_filters_ref_array( $hook_name, $args ); 737 } 738 739 /** 740 * Fires functions attached to a deprecated action hook. 741 * 742 * When an action hook is deprecated, the do_action() call is replaced with 743 * do_action_deprecated(), which triggers a deprecation notice and then fires 744 * the original hook. 745 * 746 * @since 4.6.0 747 * 748 * @see _deprecated_hook() 749 * 750 * @param string $hook_name The name of the action hook. 751 * @param list<mixed> $args Array of additional function arguments to be passed to do_action(). 752 * @param string $version The version of WordPress that deprecated the hook. 753 * @param string $replacement Optional. The hook that should have been used. Default empty. 754 * @param string $message Optional. A message regarding the change. Default empty. 755 */ 756 function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { 757 if ( ! has_action( $hook_name ) ) { 758 return; 759 } 760 761 _deprecated_hook( $hook_name, $version, $replacement, $message ); 762 763 do_action_ref_array( $hook_name, $args ); 764 } 765 766 // 767 // Functions for handling plugins. 768 // 769 770 /** 771 * Gets the basename of a plugin. 772 * 773 * This method extracts the name of a plugin from its filename. 774 * 775 * @since 1.5.0 776 * 777 * @global array $wp_plugin_paths 778 * 779 * @param string $file The filename of plugin. 780 * @return string The name of a plugin. 781 */ 782 function plugin_basename( $file ) { 783 global $wp_plugin_paths; 784 785 // $wp_plugin_paths contains normalized paths. 786 $file = wp_normalize_path( $file ); 787 788 arsort( $wp_plugin_paths ); 789 790 foreach ( $wp_plugin_paths as $dir => $realdir ) { 791 if ( str_starts_with( $file, $realdir ) ) { 792 $file = $dir . substr( $file, strlen( $realdir ) ); 793 } 794 } 795 796 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); 797 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); 798 799 // Get relative path from plugins directory. 800 $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file ); 801 $file = trim( $file, '/' ); 802 return $file; 803 } 804 805 /** 806 * Register a plugin's real path. 807 * 808 * This is used in plugin_basename() to resolve symlinked paths. 809 * 810 * @since 3.9.0 811 * 812 * @see wp_normalize_path() 813 * 814 * @global array $wp_plugin_paths 815 * 816 * @param string $file Known path to the file. 817 * @return bool Whether the path was able to be registered. 818 */ 819 function wp_register_plugin_realpath( $file ) { 820 global $wp_plugin_paths; 821 822 // Normalize, but store as static to avoid recalculation of a constant value. 823 static $wp_plugin_path = null, $wpmu_plugin_path = null; 824 825 if ( ! isset( $wp_plugin_path ) ) { 826 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR ); 827 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR ); 828 } 829 830 $plugin_path = wp_normalize_path( dirname( $file ) ); 831 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) ); 832 833 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) { 834 return false; 835 } 836 837 if ( $plugin_path !== $plugin_realpath ) { 838 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath; 839 } 840 841 return true; 842 } 843 844 /** 845 * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in. 846 * 847 * @since 2.8.0 848 * 849 * @param string $file The filename of the plugin (__FILE__). 850 * @return string the filesystem path of the directory that contains the plugin. 851 */ 852 function plugin_dir_path( $file ) { 853 return trailingslashit( dirname( $file ) ); 854 } 855 856 /** 857 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. 858 * 859 * @since 2.8.0 860 * 861 * @param string $file The filename of the plugin (__FILE__). 862 * @return string the URL path of the directory that contains the plugin. 863 */ 864 function plugin_dir_url( $file ) { 865 return trailingslashit( plugins_url( '', $file ) ); 866 } 867 868 /** 869 * Set the activation hook for a plugin. 870 * 871 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is 872 * called. In the name of this hook, PLUGINNAME is replaced with the name 873 * of the plugin, including the optional subdirectory. For example, when the 874 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then 875 * the name of this hook will become 'activate_sampleplugin/sample.php'. 876 * 877 * When the plugin consists of only one file and is (as by default) located at 878 * wp-content/plugins/sample.php the name of this hook will be 879 * 'activate_sample.php'. 880 * 881 * @since 2.0.0 882 * 883 * @param string $file The filename of the plugin including the path. 884 * @param callable $callback The function hooked to the 'activate_PLUGIN' action. 885 */ 886 function register_activation_hook( $file, $callback ) { 887 $file = plugin_basename( $file ); 888 add_action( 'activate_' . $file, $callback ); 889 } 890 891 /** 892 * Sets the deactivation hook for a plugin. 893 * 894 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is 895 * called. In the name of this hook, PLUGINNAME is replaced with the name 896 * of the plugin, including the optional subdirectory. For example, when the 897 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then 898 * the name of this hook will become 'deactivate_sampleplugin/sample.php'. 899 * 900 * When the plugin consists of only one file and is (as by default) located at 901 * wp-content/plugins/sample.php the name of this hook will be 902 * 'deactivate_sample.php'. 903 * 904 * @since 2.0.0 905 * 906 * @param string $file The filename of the plugin including the path. 907 * @param callable $callback The function hooked to the 'deactivate_PLUGIN' action. 908 */ 909 function register_deactivation_hook( $file, $callback ) { 910 $file = plugin_basename( $file ); 911 add_action( 'deactivate_' . $file, $callback ); 912 } 913 914 /** 915 * Sets the uninstallation hook for a plugin. 916 * 917 * Registers the uninstall hook that will be called when the user clicks on the 918 * uninstall link that calls for the plugin to uninstall itself. The link won't 919 * be active unless the plugin hooks into the action. 920 * 921 * The plugin should not run arbitrary code outside of functions, when 922 * registering the uninstall hook. In order to run using the hook, the plugin 923 * will have to be included, which means that any code laying outside of a 924 * function will be run during the uninstallation process. The plugin should not 925 * hinder the uninstallation process. 926 * 927 * If the plugin can not be written without running code within the plugin, then 928 * the plugin should create a file named 'uninstall.php' in the base plugin 929 * folder. This file will be called, if it exists, during the uninstallation process 930 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php' 931 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before 932 * executing. 933 * 934 * @since 2.7.0 935 * 936 * @param string $file Plugin file. 937 * @param callable $callback The callback to run when the hook is called. Must be 938 * a static method or function. 939 */ 940 function register_uninstall_hook( $file, $callback ) { 941 if ( is_array( $callback ) && is_object( $callback[0] ) ) { 942 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' ); 943 return; 944 } 945 946 /* 947 * The option should not be autoloaded, because it is not needed in most 948 * cases. Emphasis should be put on using the 'uninstall.php' way of 949 * uninstalling the plugin. 950 */ 951 $uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); 952 $plugin_basename = plugin_basename( $file ); 953 954 if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) { 955 $uninstallable_plugins[ $plugin_basename ] = $callback; 956 update_option( 'uninstall_plugins', $uninstallable_plugins ); 957 } 958 } 959 960 /** 961 * Calls the 'all' hook, which will process the functions hooked into it. 962 * 963 * The 'all' hook passes all of the arguments or parameters that were used for 964 * the hook, which this function was called for. 965 * 966 * This function is used internally for apply_filters(), do_action(), and 967 * do_action_ref_array() and is not meant to be used from outside those 968 * functions. This function does not check for the existence of the all hook, so 969 * it will fail unless the all hook exists prior to this function call. 970 * 971 * @since 2.5.0 972 * @access private 973 * 974 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 975 * 976 * @param list<mixed> $args The collected parameters from the hook that was called. 977 */ 978 function _wp_call_all_hook( $args ) { 979 global $wp_filter; 980 981 $wp_filter['all']->do_all_hook( $args ); 982 } 983 984 /** 985 * Builds a unique string ID for a hook callback function. 986 * 987 * Functions and static method callbacks are just returned as strings and 988 * shouldn't have any speed penalty. 989 * 990 * @link https://core.trac.wordpress.org/ticket/3875 991 * 992 * @since 2.2.3 993 * @since 5.3.0 Removed workarounds for spl_object_hash(). 994 * `$hook_name` and `$priority` are no longer used, 995 * and no longer returns false, but can still return void for invalid callbacks. 996 * @since 6.9.0 Returns explicit null if an invalid callback is supplied. 997 * @since 7.1.0 Uses spl_object_id() instead of spl_object_hash() for performance. 998 * @since 7.1.1 The ID for an object callback is prefixed so that it is never cast to an integer array key. 999 * 1000 * @access private 1001 * 1002 * @param string $hook_name Unused. The name of the filter to build ID for. 1003 * @param callable|string|array $callback The callback to generate ID for. The callback may 1004 * or may not exist. 1005 * @param int $priority Unused. The order in which the functions 1006 * associated with a particular action are executed. 1007 * @return string|null Unique function ID for usage as array key, or null if it couldn't be determined. 1008 * @phpstan-param Maybe_Callable $callback 1009 * @phpstan-return non-decimal-int-string|null 1010 */ 1011 function _wp_filter_build_unique_id( $hook_name, $callback, $priority ): ?string { 1012 if ( is_string( $callback ) ) { 1013 return $callback; 1014 } 1015 1016 if ( is_object( $callback ) ) { 1017 /* 1018 * The prefix keeps the ID from being the decimal representation of an integer. PHP casts such a 1019 * string to int when it is used as an array key, which would change the type of the keys in 1020 * WP_Hook::$callbacks and break consumers that pass them to string functions. 1021 */ 1022 return 'spl_object_id:' . spl_object_id( $callback ); 1023 } 1024 1025 if ( ! isset( $callback[1] ) || ! is_string( $callback[1] ) ) { 1026 return null; 1027 } 1028 1029 if ( is_object( $callback[0] ) ) { 1030 // Object class calling. 1031 return ( (string) spl_object_id( $callback[0] ) ) . $callback[1]; 1032 } elseif ( is_string( $callback[0] ) ) { 1033 // Static calling. 1034 return $callback[0] . '::' . $callback[1]; 1035 } 1036 1037 return null; 1038 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Sep 14 08:20:31 2026 | Cross-referenced by PHPXref |