求助:移除Elementor表单指定邮箱及检索所有表单名称方法
解决Elementor表单邮箱移除及表单名称检索问题
一、移除所有Elementor表单中的test@test.com邮箱接收者
你之前使用的elementor_pro/forms/actions钩子并不适合直接修改邮件接收者,正确的做法是使用专门针对邮件动作接收者的过滤器elementor_pro/forms/actions/email/recipients。替换成以下代码:
function remove_test_email_recipient( $recipients, $form, $args ) { // 将收件人转为数组(处理逗号分隔的字符串) $recipient_list = explode( ',', $recipients ); // 移除目标邮箱并去重、清理空格 $cleaned_recipients = array_filter( array_map( 'trim', $recipient_list ), function( $email ) { return $email !== 'test@test.com'; } ); // 转回逗号分隔的字符串返回 return implode( ',', $cleaned_recipients ); } add_filter( 'elementor_pro/forms/actions/email/recipients', 'remove_test_email_recipient', 10, 3 );
代码说明:
- 该钩子直接拦截邮件动作的收件人参数,处理更精准
- 兼容表单后台设置中用逗号分隔多个收件人的场景
- 自动清理邮箱前后的空格,避免误判
将代码添加到主题的functions.php文件,或者使用代码片段插件(如Code Snippets)执行即可。
二、检索网站所有Elementor表单的名称
Elementor的表单作为自定义帖子类型elementor_library存储,帖子子类型为form,可以通过两种方式获取:
方法1:使用WordPress代码查询
添加以下代码到主题functions.php或代码片段插件,执行后会在后台通知区域输出所有表单名称:
function get_all_elementor_form_names() { $form_query = new WP_Query( array( 'post_type' => 'elementor_library', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_elementor_template_type', 'value' => 'form', ), ), 'posts_per_page' => -1, ) ); if ( $form_query->have_posts() ) { echo '<div class="notice notice-info"><p>所有Elementor表单名称:</p><ul>'; while ( $form_query->have_posts() ) { $form_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul></div>'; wp_reset_postdata(); } else { echo '<div class="notice notice-warning"><p>未找到任何Elementor表单</p></div>'; } } add_action( 'admin_notices', 'get_all_elementor_form_names' );
方法2:直接查询数据库
登录数据库管理工具(如phpMyAdmin),执行以下SQL语句(注意替换wp_为你的WordPress数据库前缀):
SELECT post_title FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE wp_posts.post_type = 'elementor_library' AND wp_posts.post_status = 'publish' AND wp_postmeta.meta_key = '_elementor_template_type' AND wp_postmeta.meta_value = 'form';
内容的提问来源于stack exchange,提问作者Rodrigo Franco
相关产品推荐
相关产品推荐

