简体   繁体   English

如何(以编程方式)在向用户显示表单之前更改 cf7 字段的值

[英]how to (programmatically) change values of cf7 fields before the form is displayed to the user

I'm no expert programmer but I wouldn't want to use an entire plugin to simply put a single value from $_POST into a "readonly" field, to be displayed to the user.我不是专家程序员,但我不想使用整个插件来简单地将 $_POST 中的单个值放入“只读”字段,以显示给用户。 I could use a regular form, but I want to use CF7 because the form contains other data and I'm afraid I'll cause security problems if I dont build the whole form as CF7-secure form.我可以使用常规表单,但我想使用 CF7,因为该表单包含其他数据,如果我不将整个表单构建为 CF7 安全表单,恐怕会导致安全问题。
(I've been subscribed to 'stackoverflow' for a long time but this is my first question, inescapably stupid I guess). (我已经订阅了“stackoverflow”很长时间了,但这是我的第一个问题,我想这是不可避免的愚蠢)。

I found a way to do this with a CF7 hook but... only if the field is hidden (ie using 'wpcf7_form_hidden_fields' filter), but this only works on hidden fields.我找到了一种使用 CF7 挂钩来执行此操作的方法,但是......仅当字段被隐藏时(即使用“wpcf7_form_hidden_fields”过滤器),但这仅适用于隐藏字段。 Do I have forcely to use javascript?我必须强制使用 javascript 吗?

it seems that the problem is not interesting or that no one can give an answer.好像这个问题没什么意思或者没人能给出答案。 However I finally found a solution.但是我终于找到了解决方案。 The idea is to directly modify the 'form' field of postmeta and 'postcontent' which describe the cf7 form.这个想法是直接修改描述cf7表单的postmeta的'form'字段和'postcontent'。 Reading those fields and using a regular expression to find the right place to insert my desired value.读取这些字段并使用正则表达式找到插入所需值的正确位置。 The solution is not of a general order (but with a little work it could even become one).该解决方案不是一般顺序(但稍加工作它甚至可以成为一个)。 Applies to fields that accept a single value, and you must give an initial value - of course in quotes as expected by cf7.适用于接受单个值的字段,并且您必须提供初始值 - 当然如 cf7 所期望的那样在引号中。 That's how:这就是如何:

<?php
$my_new_value = 'my desired value';
$id_my_cf7form = xxxx; // id of th cf7 form (from shortcode)
$name_my_cf7field = 'mio-nome-campo';
                
// I get the content of the meta 'form' field of the cf7 postmeta that I want to modify 
$meta_my_cf7form = html_entity_decode(get_post_meta( $id_my_cf7form, '_form', true ));  // questa è una stringa

// I build the new content with the new title value. the regular expression looks for the first 
// occurrence of the quotation mark after the field name, the next one and then replaces the contained value
$meta_my_cf7form = preg_replace("/($name_my_cf7field\s*\b[a-zA-Z0-9]+\b\s*)(\"[^\"]*\")/", "$1\"$my_new_value\"", $meta_my_cf7form);

// and finally I update the content of the postmeta
update_post_meta( $id_my_cf7form, '_form', $meta_my_cf7form );

/* I don't know if strictly necessary, but for safety and completeness
I also update the content of the 'post_content' field of the cf7 post I want to edit */

$my_post_array = get_post( $id_my_cf7form, 'ARRAY_A' ); // this is an associative array 

// I get the post-content of the cf7 post
$my_post_content = html_entity_decode($my_post_array['post_content']);

// I build the new content with the new value
$$my_post_array['post_content']  = preg_replace("/($name_my_cf7field\s*\b[a-zA-Z0-9]+\b\s*)(\"[^\"]*\")/", "$1\"$my_new_value\"", $my_post_content);

// and I update the content of the cf7 post
wp_update_post( $my_post_array );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM