最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Handle multiple parameters in filter

programmeradmin1浏览0评论

I am trying to setup a filter which supports more than one parameter:

function FILTER_CALLBACK($block_id, array $items)
{
  // How can I pass both parameters to the next hook?
  return $items;
}
add_filter('HOOK_NAME', 'FILTER_CALLBACK', 10, 2);

Then in my plugin I do this:

apply_filters('HOOK_NAME', 'BLOCK_ID', $items);

If I then in for instance the theme using something like this:

add_filter('HOOK_NAME', function ($block_id, array $items) {
  if ($block_id == 'MY_BLOCK') {
    return array(
      'A' => 1,
    );
  }
}, 10, 2);

Then I have a problem where the $block_id is actually the array since I cannot return both parameters from the callback.

Of course I could solve this by using a single parameter like this:

$args = array(
  'block_id' => 'MY_BLOCK',
  'items' => $items,
);

And then modify the callback:

function FILTER_CALLBACK(array $options)
{
  // Here options both contains block_id and items.
  return $options;
}

But not sure if this is the correct way of doing it or if I really should use filters in this case? Perhaps an action or similar would be better? I can also say that I would like to filter the original values of the array depending on the $block_id argument.

I am trying to setup a filter which supports more than one parameter:

function FILTER_CALLBACK($block_id, array $items)
{
  // How can I pass both parameters to the next hook?
  return $items;
}
add_filter('HOOK_NAME', 'FILTER_CALLBACK', 10, 2);

Then in my plugin I do this:

apply_filters('HOOK_NAME', 'BLOCK_ID', $items);

If I then in for instance the theme using something like this:

add_filter('HOOK_NAME', function ($block_id, array $items) {
  if ($block_id == 'MY_BLOCK') {
    return array(
      'A' => 1,
    );
  }
}, 10, 2);

Then I have a problem where the $block_id is actually the array since I cannot return both parameters from the callback.

Of course I could solve this by using a single parameter like this:

$args = array(
  'block_id' => 'MY_BLOCK',
  'items' => $items,
);

And then modify the callback:

function FILTER_CALLBACK(array $options)
{
  // Here options both contains block_id and items.
  return $options;
}

But not sure if this is the correct way of doing it or if I really should use filters in this case? Perhaps an action or similar would be better? I can also say that I would like to filter the original values of the array depending on the $block_id argument.

Share Improve this question edited Jul 8, 2019 at 9:03 Cyclonecode asked Jul 8, 2019 at 8:38 CyclonecodeCyclonecode 1,1841 gold badge9 silver badges32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The problem is that you use apply_filters incorrectly.

This function takes at least two parameters:

  • $tag (string) (Required) The name of the filter hook
  • $value (mixed) (Required) The value on which the filters hooked to $tag are applied on.

So the first param should be the name of the hook and as second param you should always pass the value of filter. If you want to use any other params, you have to pass them later.

So in your case it should look like this:

function FILTER_HOOK(array $items, $block_id)
{
  // How can I pass both parameters to the next hook?
  return $items;
}
add_filter('HOOK_NAME', 'FILTER_HOOK', 10, 2);

apply_filters('HOOK_NAME', $items, 'BLOCK_ID');

add_filter('HOOK_NAME', function (array $items, $block_id) {
  if ($block_id == 'MY_BLOCK') {
    return array(
      'A' => 1,
    );
  }
  return $items; // filter should always return some value
}, 10, 2);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论