我一直在使用do\\u action在循环中生成多个动作挂钩。对我来说,do\\u操作不起作用似乎很不幸。
function ltc_generate_input_fields($fields = array()) {
echo "Hello World";
if (empty($fields) || !is_array($fields)) return;
foreach($fields as $field) {
if (!is_array($field) || empty($field)) continue;
$name = (array_key_exists(\'name\', $field)) ? $field[\'name\'] : \'\';
$key = (!empty($name)) ? strtolower(str_replace(\' \', \'_\', $name)) : \'\';
$display_name = (!empty($name)) ? ucwords($name) : \'\';
$type = (array_key_exists(\'type\', $field)) ? $field[\'type\'] : \'text\';
$id = (array_key_exists(\'id\', $field)) ? $field[\'id\'] : \'\';
$id = (empty($id)) ? strtolower(str_replace(\' \', \'-\', $name)) : $id;
$classes = (array_key_exists(\'classes\', $field)) ? $field[\'classes\'] : \'\';
?>
<div class="notebook-form-elements notebook-inputs">
<div class="notebook-inputs-wrap">
<label for="<?php echo $id; ?>"><?php echo $display_name ?></label>
<input type="<?php echo $type; ?>" id="<?php echo $id; ?>" class="notebook-features<?php echo $classes ?>" name="<?php echo $key; ?>" value="">
</div>
</div>
<?php
}
}
function ltc_generate_notebook_features_sections($sections = array()) {
if (!is_array($sections) || empty($sections)) return;
foreach($sections as $section) {
if (!is_array($section)) return;
$ltc_section_class = strtolower(str_replace(\' \', \'-\',$section[\'name\']));
$section_action = strtolower(str_replace(\' \', \'_\',$section[\'name\']));
$section_action = \'ltc_\' .$section_action .\'_content\';
$fields = (array_key_exists(\'fields\', $section)) ? $section[\'fields\'] : \'\';
?>
<div class="notebook-section section-<?php echo $ltc_section_class ?>">
<div class="notebook-section-heading">
<h3 class="notebook-title"><?php echo $section[\'name\']; ?></h3>
</div>
<div class="notebook-section-content">
<?php do_action(\'ltc_before\' . $section_action .\'_content\'); ?>
<?php do_action($section_action); ?>
<?php do_action(\'ltc_after\' . $section_action .\'_content\'); ?>
</div>
</div>
<?php
if (!is_array($fields) || empty($fields)) continue;
var_dump(has_action($section_action)); //return false
var_dump($section_action); // return \'ltc_memory_content
if (!has_action($section_action)) continue;
add_action( $section_action, \'ltc_generate_input_fields\', 5, 1);
}
}
我使用bellow函数来生成节。
function ltc_add_notebook_sections() {
$sections = array(
array(
\'name\' => \'Memory\',
\'fields\' => array(
array(
\'name\' => \'Ram Type\',
),
array(
\'name\' => \'Memory Slots\'
),
array(
\'name\' => \'Ram Speed\'
),
array(
\'name\' => \'Expandable\'
),
array(
\'name\' => \'Capacity\'
)
)
),
array(
\'name\' => \'Display Detail\'
);
ltc_generate_notebook_features_sections($sections);
}
add_action(\'ltc_notebook_features_sections\', \'ltc_add_notebook_sections\', 10, 1);
SO网友:aniash29
显然,代码有两个问题:
sections
函数中的数组ltc_add_notebook_sections
有语法错误do_action_ref_array
函数需要2个参数。您似乎只提供了ltc_generate_notebook_features_sections
功能
这两个函数可能看起来像这样:
function ltc_add_notebook_sections() {
$sections = array(
array(
\'name\' => \'Memory\',
\'fields\' => array(
array(
\'name\' => \'Ram Type\',
),
array(
\'name\' => \'Memory Slots\'
),
array(
\'name\' => \'Ram Speed\'
),
array(
\'name\' => \'Expandable\'
),
array(
\'name\' => \'Capacity\'
)
)
),
array(
\'name\' => \'Display Detail\',
),
);
ltc_generate_notebook_features_sections($sections);
}
add_action(\'ltc_notebook_features_sections\', \'ltc_add_notebook_sections\', 10, 1);
以及
function ltc_generate_notebook_features_sections($sections = array()) {
if (!is_array($sections) || empty($sections)) return;
foreach($sections as $section) {
if (!is_array($section)) return;
$ltc_section_class = strtolower(str_replace(\' \', \'-\',$section[\'name\']));
$section_action = strtolower(str_replace(\' \', \'_\',$section[\'name\']));
$section_action = \'ltc_\' .$section_action .\'_content\';
$fields = (array_key_exists(\'fields\', $section)) ? $section[\'fields\'] : \'\';
?>
<div class="notebook-section section-<?php echo $ltc_section_class ?>">
<div class="notebook-section-heading">
<h3 class="notebook-title"><?php echo $section[\'name\']; ?></h3>
</div>
<div class="notebook-section-content">
<?php do_action(\'ltc_before\' . $section_action .\'_content\'); ?>
<?php do_action_ref_array($section_action, $fields); ?>
<?php do_action(\'ltc_after\' . $section_action .\'_content\'); ?>
</div>
</div>
<?php
if (!is_array($fields) || empty($fields)) continue;
var_dump(has_action($section_action)); //return false
var_dump($section_action); // return \'ltc_memory_content
if (!has_action($section_action)) continue;
add_action( $section_action, \'ltc_generate_input_fields\', 5, 1);
}
}
这可能会解决它!