0

I've been using do_action to generate multiple action hooks in the loop. It seems unlucky for me the do_action not working.

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);

    }
}

I used the bellow function to generate section.

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);
0

You are calling do_action before adding it. Move add_action up, before HTML part.

Second, your action function ltc_generate_input_fields' expects$fieldsvalue, however yourdo_action` does not pass this parameter, so change it to

do_action($section_actions, $fields);
| improve this answer | |
  • Thanks. it works. – Kimsea Sok Jan 27 at 13:18
0

Two issues with the code apparently:

  1. sections array in the function ltc_add_notebook_sections has a syntax error.
  2. do_action_ref_array function requires 2 parameters. You only seem to be supplying 1 in the ltc_generate_notebook_features_sections function.

The two functions should probably look something like this:

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);

and

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);

    }
}

That should probably fix it!

| improve this answer | |
  • I'm sorry. It's the mistake when I copy the code to stackexchange. I edited the code, could please recheck this again. – Kimsea Sok Jan 27 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.