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