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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀