Chris Gylseth
Chris Gylseth

Reputation: 11

Use image as radio button on Contact Form 7

I'm trying to create an option for people to select an image in a WP CF7 form. As they will only be able to select one option, it seems to me that using a radio button function is the best way to go. I found an example of a code on https://wpquestions.com/Need_Image_as_a_Radio_Button_in_Contact_Form_7/19618#answer_16362 but adding the code does a) not create a tag in the admin section, and b) only returns the full shortcode on the page, instead of returning the desired images.

I did find this How to make custom form-tag in contact form 7 required here on this forum. I tried adding the

add_action( 'wpcf7_init', 'wpcf7_add_form_tag_imageradio' );

But it returned the following error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wpcf7_add_form_tag_imageradio' not found or invalid function name in /home2/clay/public_html/wp-includes/class-wp-hook.php on line 298

Any ideas on how to solve this? Theme used is OnePage Express

Upvotes: 1

Views: 8739

Answers (2)

Benjamin
Benjamin

Reputation: 41

You need to put both arguments:

function add_shortcode_imageradio() {
    wpcf7_add_shortcode( 'imageradio', 'imageradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imageradio' );

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

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

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

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

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

    return $datalist;
}

Dont forget CSS:

input.hideradio{ /* HIDE RADIO */
    visibility: hidden; /* Makes input not-clickable */
    position: absolute; /* Remove input from document flow */
}
.imgradio label > input + img{ /* IMAGE STYLES */
    cursor:pointer;
    border:2px solid transparent;
}
.imgradio label > input:checked + img{ /* (RADIO CHECKED) IMAGE STYLES */
    border:2px solid #f00;
}

And use the new shortcode declared inside FORM tab in contactform7:

[imageradio radio-312 
"Value1!http://[YourImgUrl]" 
"Value2!http://[YourImgUrl]"
]

In the example Need Image as... they missed to inclcue "add_action" that's all

You can check CF7 doc also

Hope Helps!

Upvotes: 3

lelebart
lelebart

Reputation: 1

try adding instead

add_action( 'wpcf7_init', 'add_shortcode_imageradio' );

Upvotes: 0

Related Questions