根据是否选中复选框,在提交时返回0或1-联系表7

时间:2019-02-01 17:19:40

标签: php wordpress contact-form-7

I“米试图改变接触形式7如何发送一个复选框的值我如果该复选框被选中,则该复选框值是1,否则该复选框的值等于0;

我已经试过低于该只是返回1中的代码而不管该复选框是否已被检查或没有。

function action_wpcf7_posted_data( $array ) {     
    if ($array['optinsms'] == "" ) {
        $array['optinsms'] = 0;
    } else {
        $array['optinsms'] = 1;
    }
    return $array;
}

add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1);

任何帮助将不胜感激!

干杯

碧玉

1 个答案:

答案 0 :(得分:1)

您非常亲密。我相信这里的问题是,复选框响应将始终是一个数组。所以您需要检查第一个索引-

function action_wpcf7_posted_data( $array ) {     
    if ($array['optinsms'][0] == "" ) {
        $array['optinsms'][0] = 0;
    } else {
        $array['optinsms'][0] = 1;
    }
    return $array;
}
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1)

这应该有效。

谢谢。