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