当然,有办法解决这个问题。
您需要将提交的数据保存到transient 并从重定向页面访问它,您可以确认访问者实际填写了表单。不需要私人页面,而是使用自定义页面模板来查看受限制的页面,并检查用户是否在页面模板顶部拥有访问权限,否则将重定向这些权限。
你可以通过,
步骤1:确定当前提交的内容(这甚至适用于未登录的用户。)使用WPnonce 作为表单中的隐藏字段,并放置一个js脚本以重定向到页面successful submission event 使用anonymous function 挂在cf7上do_shortcode_tag
陈列filter,
add_filter(\'wpcf7_form_hidden_fields\',\'add_hidden_nonce\');
function add_hidden_nonce($fields){
$nonce = wp_create_nonce(\'cf7-redirect-id\');
$fields[\'redirect_nonce\'] = $nonce;
/*
hook the shortcode tag filter using an anonymous function
in order to use the same nonce and place a redirect js event
script at the end of the form.
*/
add_filter(\'do_shortcode_tag\', function($output, $tag, $attrs) use ($nonce){
//check this is your form, assuming form id = 1, replace it with your id.
if($tag != "contact-form-7" || $attrs[\'id\']!= 1) return $output;
$script = \'<script>\'.PHP_EOL;
$script .= \'document.addEventListener( "wpcf7mailsent", function( event ){\'.PHP_EOL;
//add your redirect page url with the nonce as an attribute.
$script .= \' location = "http://example.com/submitted/?cf7="\'.$nonce.\';\'.PHP_EOL;
$script .= \' }\'.PHP_EOL;
$script .= \'</script>\'.PHP_EOL;
return $output.PHP_EOL.$script;
},10,3);
return $fields;
}
步骤2:提交通过验证/发送电子邮件后,挂接cf7操作“wpcf7\\u mail\\u sent”。
add_action(\'wpcf7_mail_sent\', \'save_posted_data_into_transient\');
function save_posted_data_into_transient(){
if(isset($_POST[\'redirect_nonce\'])){ //save the data.
//save the posted data from the form. Note if you have submitted file fields you will also need to store the $_FILES array.
set_transient(\'_cf7_data_\'.$_POST[\'redirect_nonce\'], $_POST, 5*60); //5 min expiration.
}
}
步骤3:在重定向自定义页面上,您现在可以
access your stored transient 数据,将其放置在自定义页面模板的顶部,
<?php
if( isset($_GET[\'cf7\']) ){
$transient = \'_cf7_data_\'.$_GET[\'cf7\'];
$data = get_transient($transient);
//$data[\'my-text-field\']....
}
?>
如果瞬态缺失、无效或cf7 nonce属性缺失,则只需
redirect 通知用户必须填写表单才能访问该页面。