do_action not working in loop

时间:2020-01-27 作者:Kimsea Sok

我一直在使用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);

2 个回复
最合适的回答,由SO网友:Vitauts Stočka 整理而成

您正在呼叫do_action 添加之前。移动add_action 向上,HTML部分之前。

第二,你的行动功能ltc_generate_input_fields\' expects$字段value, however yourdo\\u action `不传递此参数,请将其更改为

do_action($section_actions, $fields);

SO网友:aniash29

显然,代码有两个问题:

  1. sections 函数中的数组ltc_add_notebook_sections 有语法错误
  2. 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);

    }
}
这可能会解决它!

相关推荐

OOP development and hooks

我目前正在为Wordpress编写我的第一个OOP插件。为了帮助我找到一点结构,a boiler plate 这为我奠定了基础。在里面Main.php 有一种方法可以为管理员加载JS和CSS资产:/** * Register all of the hooks related to the admin area functionality * of the plugin. * * @since 0.1.0 * @access private