Support » Plugin: Conditional Fields for Contact Form 7 » BUG: Plugin overwrite cf7 hidden fields

  • Resolved Aurovrata Venet

    (@aurovrata)


    The file cf7cf.php hooks the cf7 filter ‘wpcf7_form_hidden_fields’ on line 290, but instead of returning an updated set of hiddend fields, overwrites it completly, breaking other plugins using the same functionality,

    add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
    
    function wpcf7cf_form_hidden_fields($hidden_fields) {
    
        $current_form = wpcf7_get_current_contact_form();
        $current_form_id = $current_form->id();
    
        $options = array(
            'form_id' => $current_form_id,
            'conditions' => get_post_meta($current_form_id,'wpcf7cf_options', true),
            'settings' => get_option(WPCF7CF_OPTIONS)
        );
    
        return array(
            '_wpcf7cf_hidden_group_fields' => '',
            '_wpcf7cf_hidden_groups' => '',
            '_wpcf7cf_visible_groups' => '',
            '_wpcf7cf_options' => ''.json_encode($options),
        );
    }

    it should return instead,

    
    return array_merge($hidden_fields, array(
            '_wpcf7cf_hidden_group_fields' => '',
            '_wpcf7cf_hidden_groups' => '',
            '_wpcf7cf_visible_groups' => '',
            '_wpcf7cf_options' => ''.json_encode($options),
        ));
    
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Jules Colle

    (@jules-colle)

    that looks like a bad bug indeed. thanks for reporting it. will fix it ASAP.

    Plugin Author Jules Colle

    (@jules-colle)

    I just checked, but it looks like it’s okay. Contact Form 7 is calling the filter like this in includes/contact-form.php line 428:

    $hidden_fields += (array) apply_filters('wpcf7_form_hidden_fields', array() );

    As you can see it passes an empty array as parameter, making it kind of obsolete, and merges the returned array with the hidden fields anyway. So I don’t see the difference with merging the results-array with an empty array, or simply returning the array like I do now.

    Correct me if you think I’m wrong.

    Hi Jules, thank you for the prompt reply

    it passes an empty array as parameter, making it kind of obsolete, and merges the returned array with the hidden fields anyway.

    that is true, but CF7 does this in order to prevent such kind of coding overwriting its hidden fields

    I don’t see the difference with merging the results-array with an empty array, or simply returning the array like I do now

    well since your function is hooked on the default priority, this means that any other plugin that uses this hook to include some custom hidden fields runs the risk of either having its hidden fields wiped (if hooked prior to your function), or potentially even worse for your plugin, wiping your plugin’s fields if its hooked after your function and assumes a similar logic.

    I have 2 plugins that use this hook on the default priority, both of which simply appends custom fields to the end of the array. In one installation my plugin’s function happened to be hooked after yours (although they both have priority 10, it all depends on when the registration is executed) and therefore my custom fields were being systematically overwritten.

    Generally it is always best to return the *modified* value of a filter hook in WordPress, rather than overwriting it, as there may be several plugins/theme functions that hook onto it.

    Plugin Author Jules Colle

    (@jules-colle)

    You’re right. I wasn’t thinking clearly. Thanks again for pointing this out and taking your time to explain. the code will be updated in the next version.

    hey, no worries! thanks for the udpate πŸ™‚

    Plugin Author Jules Colle

    (@jules-colle)

    do u want me to raise the same issue on your github repo?

    Plugin Author Jules Colle

    (@jules-colle)

    no, I already did. I just posted the wrong link here. The post is: https://github.com/pwkip/contact-form-7-conditional-fields/issues/20

    ok, got it. thanks. Did you get a chance to take a look at this issue? Am happy to give you some suggestions on Github if you want to look at this.

    Plugin Author Jules Colle

    (@jules-colle)

    No, it’s okay. I know what to do. Working on a major update, but I need to do some more proper testing, so the release keeps getting postponed πŸ™

    ok great! Feel free to ping me on my mail if you need any clarification.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘BUG: Plugin overwrite cf7 hidden fields’ is closed to new replies.