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

php - WP add_action factory

programmeradmin1浏览0评论

Hello I am new to WP and PHP, I am trying to create a class that takes (maybe) an array of arrays, then used to generate N° add_action for each array passed.

Example:

[
  array(
  "name" => "my_custom_action",
  "cb" => "function(){return true;}" 
)
]

This is the code I wrote until now.

class Actions {

    public function __construct($actions){
      $this->actions = $actions;

      foreach($this->actions as $action){
        add_action($action['name'], $action['cb']);
      }
    }

  }

I was reading about the create_function() method, but it's deprecated.

I would like to understand how to create multiple add_action with a single call basically, instead of writing every time the function and then add the action in the constructor.

I also found this: .anonymous.php

It's used to generate functions instead of using create_function(), but I do not understand it very well.

Hello I am new to WP and PHP, I am trying to create a class that takes (maybe) an array of arrays, then used to generate N° add_action for each array passed.

Example:

[
  array(
  "name" => "my_custom_action",
  "cb" => "function(){return true;}" 
)
]

This is the code I wrote until now.

class Actions {

    public function __construct($actions){
      $this->actions = $actions;

      foreach($this->actions as $action){
        add_action($action['name'], $action['cb']);
      }
    }

  }

I was reading about the create_function() method, but it's deprecated.

I would like to understand how to create multiple add_action with a single call basically, instead of writing every time the function and then add the action in the constructor.

I also found this: https://www.php/manual/en/functions.anonymous.php

It's used to generate functions instead of using create_function(), but I do not understand it very well.

Share Improve this question asked Feb 25, 2020 at 10:54 d0t_md0t_m 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Check out the php callable manual.

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

However, in your code sample you are using an anonymous function, which can be passed directly.

[ array( "name" => "my_custom_action", "cb" => function(){return true;} ) ]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论