0

有这个名为 ninja forms 的 wordpress 插件,http://developer.ninjaforms.com/codex/merge-tags/

        /* Individual tag registration. */
        $this->merge_tags = array(

            'foo' => array(
              'id' => 'foo',
              'tag' => '{my:foo}', // The tag to be  used.
              'label' => __( 'Foo', 'my_plugin' ), // Translatable label for tag selection.
              'callback' => 'foo' // Class method for processing the tag. See below.
          ),
        );

        /*
         * Use the `init` and `admin_init` hooks for any necessary data setup that relies on WordPress.
         * See: https://codex.wordpress.org/Plugin_API/Action_Reference
         */
        add_action( 'init', array( $this, 'init' ) );
        add_action( 'admin_init', array( $this, 'admin_init' ) );
      }

      public function init(){ /* This section intentionally left blank. */ }
      public function admin_init(){ /* This section intentionally left blank. */ }

      /**
       * The callback method for the {my:foo} merge tag.
       * @return string
       */
      public function foo()
      {
        // Do stuff here.
        return 'bar';

} }

然后将“回调”的值用作函数,公共函数(foo)。

我已将此添加到数组中:

[foo] => Array
    (
        [id] => foo
        [tag] => {my:foo}
        [label] => Foo
        [callback] => foo
    )

[surveyid] => Array
    (
        [id] => surveyid
        [tag] => {my:surveyid}
        [label] => Surveyid
        [callback] => surveyid
    )

[membername] => Array
    (
        [id] => membername
        [tag] => {my:membername}
        [label] => Membername
        [callback] => membername
    )

我已经向这个数组添加了更多具有相同格式的数组,并且我喜欢将它们的“回调”值设置为公共函数。

 /**
   * The callback method for the {my:foo} merge tag.
   * @return string
   */
  public function foo()
  {
    // Do stuff here.
    return 'bar';
  }

虽然我计划多次这样做,但将来我可能会添加更多数组。所以我试图为每个数组回调值动态分配公共函数。

这就是我所拥有的。

        $data = array(

        '@attributes' => array(
            'surveyid' => 'V54236',
            'membername' => 'John Smith',

            ));

    $realThing = array();
    foreach($data['@attributes'] as $key => $value) {    
        $realThing[$key] = array(
            'id' =>  $key,
            'tag' => '{my:'.$key.'}', 
            'label' => __( ucfirst($key), 'my_plugin' ),
            'callback' => $key

   );
}

    $this->merge_tags = $realThing;

        add_action( 'init', array( $this, 'init' ) );
        add_action( 'admin_init', array( $this, 'admin_init' ) );
      }

      public function init(){ /* This section intentionally left blank. */ }
      public function admin_init(){ /* This section intentionally left blank. */ }

    }

我尝试为每个回调值分配函数。

        foreach($realThing as $key => $value){
             public function $key['callback'](){
                 return $data['@attributes'][$key];
             }
         };

所需的输出:

  public function foo()
  {
    // Do stuff here.
    return 'bar';
  }

  public function surveyid()
  {
    // Do stuff here.
    return 'V54236';

  public function membername()
  {
    // Do stuff here.
    return 'John Smith';

所有帮助表示赞赏。

还得到:语法错误,意外的'foreach'(T_FOREACH),期待函数(T_FUNCTION)

4

1 回答 1

0

你在处理数据时有一些错误,你应该只声明每个函数一次:

foreach($realThing as $key => $value){
         if(!function_exists($value['callback'])){
             public function $value['callback'](){
                 return $data['@attributes'][$key];
             }
          }
 };

但是,此代码不起作用,因为函数声明中不允许使用变量。以下代码应该可以工作,但是您必须以不同的方式调用回调,因为它存储在数组中:

$callbacks = array();
foreach($realThing as $key => $value){
         if(!isset($callbacks[$value['callback']])){
             $callbacks[$value['callback']] = function () use ($data, $key){
                 return $data['@attributes'][$key];
             };
          }
 };
 unset($key);

 echo $callbacks["surveyid"]();

我很确定尽管您可以通过其他方式做您想做的事情。

于 2017-08-07T15:01:02.283 回答