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