Return 提交时为 0 或 1,具体取决于是否选中复选框 - 联系表 7

Return 0 or 1 on submit depending on whether a checkbox is checked - Contact Form 7

我正在尝试更改联系表 7 发送复选框值的方式。我是如果复选框已被选中,则复选框值为 1,否则复选框值等于 0;

我试过下面的代码,无论复选框是否被选中,都只是 returns 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);

如有任何帮助,我们将不胜感激!

干杯,

贾斯珀

你很接近。我相信这里的问题是,复选框响应将始终是一个数组。所以你需要检查第一个索引-

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)

这应该有效。

谢谢。