WordPress Contact Form7插件wpcf7_before_send_mail钩子is_page条件失效
解决Contact Form 7钩子中
is_page()条件判断失效的问题 问题根源
Contact Form 7的表单提交默认采用AJAX异步请求,当wpcf7_before_send_mail钩子触发时,WordPress的全局$post上下文已经切换到AJAX请求的内部处理环境,不再指向表单所在的页面,因此依赖全局$post的is_page()函数自然无法生效。
可行的PHP解决方案
方法1:通过隐藏字段传递页面ID(最可靠)
在表单所在页面添加隐藏字段存储当前页面ID,提交时随表单数据一起发送,再在钩子中读取该字段进行判断。
- 在CF7表单编辑器中添加隐藏字段:
[hidden page_id default:get_the_ID]
该字段会自动获取当前页面ID并作为表单数据提交。
- 修改
functions.php中的代码:
add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 ); function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){ $submission = WPCF7_Submission::get_instance(); if ( $submission ){ $posted_data = $submission->get_posted_data(); if ( empty( $posted_data ) ){ return; } $name_data = $posted_data['your-name']; // 读取隐藏字段中的页面ID $current_page_id = isset($posted_data['page_id']) ? intval($posted_data['page_id']) : 0; if( $current_page_id === 41235 ) { $changed_name = 'something with is page condition'; } else { $changed_name = 'something is in else condition'; } $mail = $WPCF7_ContactForm->prop( 'mail' ); $new_mail = str_replace( '[your-name]', $changed_name, $mail ); $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) ); } return $WPCF7_ContactForm; }
方法2:通过表单关联页面查询判断
如果表单固定嵌入在特定页面,可直接查询使用该表单的页面ID进行匹配:
add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 ); function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){ $submission = WPCF7_Submission::get_instance(); if ( $submission ){ $posted_data = $submission->get_posted_data(); if ( empty( $posted_data ) ){ return; } $name_data = $posted_data['your-name']; // 获取当前CF7表单ID $form_id = $WPCF7_ContactForm->id(); // 查询嵌入该表单的页面 $target_page_found = false; $pages = get_posts([ 'post_type' => 'page', 'posts_per_page' => -1, 's' => '[contact-form-7 id="' . $form_id . '"' ]); foreach($pages as $page){ if($page->ID === 41235){ $target_page_found = true; break; } } if( $target_page_found ) { $changed_name = 'something with is page condition'; } else { $changed_name = 'something is in else condition'; } $mail = $WPCF7_ContactForm->prop( 'mail' ); $new_mail = str_replace( '[your-name]', $changed_name, $mail ); $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) ); } return $WPCF7_ContactForm; }
注:此方法依赖表单以短代码形式嵌入页面,若表单是通过模板文件直接调用,需调整查询逻辑匹配实际嵌入方式。
方法3:通过请求来源URL解析页面ID
读取请求的Referer头,解析出来源页面的ID:
add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 ); function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){ $submission = WPCF7_Submission::get_instance(); if ( $submission ){ $posted_data = $submission->get_posted_data(); if ( empty( $posted_data ) ){ return; } $name_data = $posted_data['your-name']; $current_page_id = 0; // 读取请求来源URL $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; if($referer){ $url_parts = parse_url($referer); // 解析查询参数获取页面ID if(isset($url_parts['query'])){ parse_str($url_parts['query'], $query_vars); $current_page_id = isset($query_vars['page_id']) ? intval($query_vars['page_id']) : 0; if(empty($current_page_id) && isset($query_vars['p'])){ $current_page_id = intval($query_vars['p']); } } // 固定链接场景下通过slug获取ID if(empty($current_page_id) && isset($url_parts['path'])){ $slug = trim($url_parts['path'], '/'); $page = get_page_by_path($slug); $current_page_id = $page ? $page->ID : 0; } } if( $current_page_id === 41235 ) { $changed_name = 'something with is page condition'; } else { $changed_name = 'something is in else condition'; } $mail = $WPCF7_ContactForm->prop( 'mail' ); $new_mail = str_replace( '[your-name]', $changed_name, $mail ); $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) ); } return $WPCF7_ContactForm; }
注:此方法依赖浏览器发送的Referer头,部分安全设置可能会屏蔽该字段,稳定性不如方法1。
内容的提问来源于stack exchange,提问作者Shakibrkhan
相关产品推荐
相关产品推荐

