duanqianruan8448
2017-03-13 07:07 阅读 37
已采纳

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 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.

    点赞 评论 复制链接分享

相关推荐