duanqianruan8448 2017-03-13 07:07
浏览 40
已采纳

CF7动态更改表单数据

I am trying to extend the CF7 plugin through their form tag filter to meet my needs, however I have run into a weird case. I have the following function (In my Theme Functions file) that somewhat works:

function custom_shortcode_handler( $tag, $unused ) {
    $formidable_array = get_related_vars(array('type' => 'cf7'));

    if ($tag[name] === 'user') {
        $tag[raw_values][] = $user;
        $tag[values][] = $user;
        $tag[labels][] = $user;
    }
    if ($tag[name] === 'display-name') {
        $tag[raw_values][] = $display_name;
        $tag[values][] = $display_name;
        $tag[labels][] = $display_name;
    }
    if ($tag[name] === 'email') {
        $tag[raw_values][] = $formidable_array[139];
        $tag[values][] = $formidable_array[139];
        $tag[labels][] = $formidable_array[139];
        if (!empty($formidable_array[223])) {
            $tag[raw_values][] = $formidable_array[139].', '.$formidable_array[298];
            $tag[values][] = $formidable_array[139].', '.$formidable_array[298];
            $tag[labels][] = $formidable_array[139].', '.$formidable_array[298];
        }
    }
    if ($tag[name] === 'phone') {
        $tag[raw_values][] = $formidable_array[135];
        $tag[values][] = $formidable_array[135];
        $tag[labels][] = $formidable_array[135];
    }
    if ($tag[name] === 'campaign') {
        $tag[raw_values][] = $campaign_name;
        $tag[values][] = $campaign_name;
        $tag[labels][] = $campaign_name;
    }

    return $tag;
}
add_filter('wpcf7_form_tag', 'custom_shortcode_handler', 10, 2);

On the Contact Form 7 side, I have the following fields:

[text user]
[text display-name]
[text email]
[text phone]
[text campaign]

Each one of these text fields is correctly updated by the function above, except for the phone field. I've tried switching that field to tel, number, text area, hidden, to no avail. It just doesn't retrieve the value, but if I replace $formidable_array[135] with any string (that has no numbers) it would work. If I put in any digits, it doesn't anymore or practically just takes out the digits and leaves the letters.

Do keep in mind that $formidable_array[135] is not the issue because it can be replaced by any number inside quotes and the output remains the same.

Something weird to note is that if I were to print out the $tag array within the if-statement as such:

    if ($tag[name] === 'phone') {
                $tag[raw_values][] = $formidable_array[135];
                $tag[values][] = $formidable_array[135];
                $tag[labels][] = $formidable_array[135];
                echo '<pre>';
                print_r($tag);
                echo '</pre>';
            }

It would print the $tag array twice, first with the correctly inserted phone values (i.e. raw_values, values, and labels all set to '1234567890'), followed by a repost with all the values set back to null.

I am somewhat convinced that they've some kind of Regex cleaner within the plugin code for digits for whatever reason but I cannot find it or figure out a way to resolve this.

Any help is much appreciated.

EDIT:

Seems that if I replace $formidable_array[135] with the following:

    if ($tag[name] === 'phone') {
                $tag[raw_values][] = '123-123-1234';
                $tag[values][] = '123-123-1234';
                $tag[labels][] = '123-123-1234';
            }

It works... Don't get why though, because $formidable_array[135] holds the same value if echo'd instead.

EDIT:

I need to figure out what's calling my function a 2nd time, doing a var_dump() on my $formidable_array(135) made me discover that the 2nd time that variable is printed out, it's NULL. So the "wpcf7_form_tag" hook is getting executed twice??

  • 写回答

1条回答 默认 最新

  • donglun2010 2017-03-19 23:54
    关注

    The "wpcf7_form_tag" hook isn't being executed twice, but CF7 is detecting each of the field names twice, not sure why. But that wasn't the cause for eventually having NULL values...

    The phone number wasn't the only thing that was becoming NULL, my entire formidable array was becoming NULL.

    My problem was related to the way I obtained my formidable values from the Database. I was creating a new DB connection each time my function ran, which was a big NoNo. Just made my external DB connection a global one, and problem disappeared.

    I think it was a speed issue, since there were two functions trying to query the same database table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图