| | 338 | * Hooks a function to a specific action, AND allows you to pass custom arguments to that function |
| | 339 | * |
| | 340 | * @uses Add_Action_Handler to prop up our function and send it the args |
| | 341 | * |
| | 342 | * @package WordPress |
| | 343 | * @subpackage Plugin |
| | 344 | * |
| | 345 | * @param string $tag |
| | 346 | * @param callback $callback |
| | 347 | * @param int $priority |
| | 348 | * @param null $args one or more arguments to pass to hooked function. |
| | 349 | */ |
| | 350 | function add_action_with_args( $tag, $callback, $priority = 10, $args = null ) { |
| | 351 | |
| | 352 | // Accept as many different args as we're passed |
| | 353 | $args = array_slice(func_get_args(), 3); |
| | 354 | |
| | 355 | return add_action( $tag, array( new Add_Action_Handler( $args, $callback ), 'action' ), $priority, 1 ); |
| | 356 | } |
| | 357 | |
| | 358 | /** |
| | 359 | * Helper class for add_action_with_args() |
| | 360 | * |
| | 361 | * @uses Add_Action_Handler to prop up our function and send it the args |
| | 362 | * |
| | 363 | * @package WordPress |
| | 364 | * @subpackage Plugin |
| | 365 | * |
| | 366 | * @param array $args Arguments to pass through to a given callback |
| | 367 | * @param string $callback The callback to run |
| | 368 | */ |
| | 369 | class Add_Action_Handler { |
| | 370 | |
| | 371 | private $args; |
| | 372 | private $callback; |
| | 373 | |
| | 374 | function __construct($args, $callback = null) { |
| | 375 | |
| | 376 | $this->args = $args; |
| | 377 | $this->callback = $callback; |
| | 378 | } |
| | 379 | |
| | 380 | function action() { |
| | 381 | |
| | 382 | call_user_func_array($this->callback, $this->args); |
| | 383 | |
| | 384 | if( func_num_args() ) |
| | 385 | return func_get_arg(0); |
| | 386 | |
| | 387 | return null; |
| | 388 | } |
| | 389 | } |
| | 390 | |
| | 391 | /** |