我不是专业程序员,但我不想使用整个插件来简单地将$_POST中的单个值放入"readonly"字段,显示给用户。我可以使用常规表单,但我想使用CF7,因为表单包含其他数据,我担心如果我不将整个表单构建为CF7安全表单,我会导致安全问题。
(我订阅'stackoverflow'很长时间了,但这是我的第一个问题,我猜不可避免的愚蠢)。
我找到了一种方法来做到这一点与CF7挂钩,但…只有当字段是隐藏的(即使用'wpcf7_form_hidden_fields'过滤器),但这只适用于隐藏字段。我必须强制使用javascript吗?
这个问题似乎没什么意思,没有人能给出答案。然而,我终于找到了一个解决方案。这个想法是直接修改描述cf7表单的postmeta和postcontent的"form"字段。读取这些字段并使用正则表达式找到插入所需值的正确位置。这个解决方案并不是一个一般的顺序(但稍加努力,它甚至可以成为一个)。适用于接受单个值的字段,并且必须给出初始值—当然,cf7所期望的是在引号中。这就是:
<?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_cf7fields*b[a-zA-Z0-9]+bs*)("[^"]*")/", "$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_cf7fields*b[a-zA-Z0-9]+bs*)("[^"]*")/", "$1"$my_new_value"", $my_post_content);
// and I update the content of the cf7 post
wp_update_post( $my_post_array );