Theres这个wordpress插件名为ninja forms,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';
} }
然后将'callback'的值用作函数public function(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
)
我已经为这个数组添加了更多具有相同格式的数组,并且id喜欢将它们的'回调'值设置为公共函数。
/**
* 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)
答案 0 :(得分: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"]();
我很确定你可以通过其他方式做你想做的事。