联系表 7 - 多文件上传值作为电子邮件中的链接

Contact form 7- Multi File Upload Values as Links in email

我在表单中使用 'Contact Form 7 Drag and Drop FIles Upload - Multiple Files Upload' (https://codecanyon.net/item/contact-form-7-drag-and-drop-files-upload-multiple-files-upload/20683653) 插件。上传的文件可能很大,所以我不想将其附加到邮件中,而是将 links 附加到邮件中上传的文件中。

我去掉了邮件附件栏的tag,添加到邮件正文中,希望能输出上传的文件links:

 <p><strong>IMAGES</strong><br/><br/>[dropfiles-291]</a></p>

但它只输出以“|”分隔的文件名。例如:'imagename1.jpg|imagename2.jpg|imagename3.jpg|imagename4.jpg'。

经过一番搜索,我在拖放插件代码中找到了这段代码:

add_filter('wpcf7_mail_tag_replaced_dropfiles', array($this, 'wpcf7_mail_tag_replaced'), 100, 3);

function wpcf7_mail_tag_replaced($text, $submitted, $html ){
    $upload_dir   = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);
    }else{
        return implode(" | ", $text_custom);
    }           

}

我不是 php 开发人员,但代码在我看来应该用电子邮件中的 link 包装上传的文件。但是,它不起作用。经过广泛的在线研究后,我将代码更改为:

add_filter('wpcf7_mail_tag_replaced', array($this, 'wpcf7_mail_tag_replaced_dropfiles'), 100, 3);

function wpcf7_mail_tag_replaced_dropfiles( $text, $submitted, $html ){        

    $upload_dir = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);}
        else{return implode(" | ", $text_custom);}               

}

电子邮件现在具有所需的 link 效果,但作用于表单中所有提交的字段,而不仅仅是 [dropfiles] 字段。 [dropfiles] 字段看起来很棒,每个上传文件的 link 在一个新行等。但我显然不希望普通文本、文本区域、复选框、收音机等字段也为 links.

我已经向插件开发者发送了支持请求,但还没有收到任何回复。我可以采取什么方法来实现这一目标?

我找到了临时解决方案 - 直到我从插件开发人员那里获得永久解决方案。我将代码更改如下:

add_filter('wpcf7_mail_tag_replaced', array($this, 'wpcf7_mail_tag_replaced_dropfiles'), 100, 3);

function wpcf7_mail_tag_replaced_dropfiles( $text, $submitted, $html ){        
if(preg_match('/\.(jpg|png|jpeg|gif)$/', $submitted))  {
    $upload_dir = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);}
        else{return implode(" | ", $text_custom);}               
    } 

    return $text; 
}

我花了 HOURS 来解决这个问题,但我想我已经找到了问题所在。

我注意到它只是在我将 dropfiles 标记用作必填字段时发生的。 [dropfiles* ...]

Contact Form 7 具有应用过滤器 "wpcf7_mail_tag_replaced_{$type}" 的函数 "replace_tags_callback"。

问题是 {$type} 是从 $form_tag->type 获取的,在我们的例子中可以是 "dropfiles" 或 "dropfiles*"。如果 CF7 使用 $form_tag->basetype 得到它,那么两个标签都是一样的。

所以为了解决这个问题,我在 contact-form-7-drop-files/frontend/index.php

的第 7 行为 "dropfiles*" 添加了一个过滤器
add_filter('wpcf7_mail_tag_replaced_dropfiles*', array($this, 'wpcf7_mail_tag_replaced'), 100, 3);

所以class的开头现在是这样的:

function __construct(){
    add_action("wp_enqueue_scripts",array($this,"add_lib"));
    add_filter('wpcf7_form_response_output', array($this,'add_settings'),999999,4);
    add_filter('wpcf7_mail_tag_replaced_dropfiles', array($this, 'wpcf7_mail_tag_replaced'), 100, 3);
    add_filter('wpcf7_mail_tag_replaced_dropfiles*', array($this, 'wpcf7_mail_tag_replaced'), 100, 3);
}
function wpcf7_mail_tag_replaced($text, $submitted, $html ){
    $upload_dir   = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);
    }else{
        return implode(" | ", $text_custom);
    }



}

我向插件作者报告了这个错误,希望he/she在下一个版本中更新它。