Search code examples
wordpresscontact-form-7

Image as a checkbox option in Contact Form 7


I wanted to use the image as an option to check in contact form 7. I searched and found the way to do that with radio button. I made the changes in the code, and it does work as a checkbox, but it is only sending one value when submitting the form, not the multiple values.

This is the code I am using. Please tell me what needs to be changed.

function add_shortcode_imagecheckbox() {
        wpcf7_add_shortcode( 'imagecheckbox', 'imagecheckbox_handler', true );
    }
    add_action( 'wpcf7_init', 'add_shortcode_imagecheckbox' );

    function imagecheckbox_handler( $tag ){
        $tag = new WPCF7_FormTag( $tag );

        $atts = array(
            'type' => 'checkbox',
            'name' => $tag->name,
            'list' => $tag->name . '-options' );

        $input = sprintf(
            '<input %s />',
            wpcf7_format_atts( $atts ) );
            $datalist = '';
            $datalist .= '<div class="imgcheckbox">';
            foreach ( $tag->values as $val ) {
            list($checkboxvalue,$imagepath) = explode("!", $val
        );

        $datalist .= sprintf(
         '<label><input type="checkbox" name="%s" value="%s" class="hidecheckbox" /><img src="%s"></label>', $tag->name, $checkboxvalue, $imagepath 
        );

        }
        $datalist .= '</div>';

        return $datalist;
    }

Solution

  • it's a bit late but I've had this problem right now

    in input name add []

           $datalist .= sprintf(
         '<label><input type="checkbox" name="%s[]" value="%s" class="hidecheckbox" /><img src="%s"></label>', $tag->name, $checkboxvalue, $imagepath 
        );