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

时间:2019-02-22 09:47:56

标签: php wordpress contact-form-7

我在我的表单中使用了“联系表单7拖放文件上传-多个文件上传”(https://codecanyon.net/item/contact-form-7-drag-and-drop-files-upload-multiple-files-upload/20683653)插件。上传的内容可能很大,因此我不希望将其附加到邮件中,而应具有指向邮件中已上传文件的链接。

我从邮件附件字段中删除了标签,并将其添加到邮件正文中,希望它可以输出上传的文件链接:

 <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开发人员,但是代码在我看来就像应该用电子邮件中的链接包装上载的文件一样。但是,它不起作用。经过广泛的在线研究,我将代码更改为:

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);}               

}

电子邮件现在具有所需的链接效果,但不仅限于[dropfiles]字段,还包含在表单中所有提交的字段上。 [dropfiles]字段看起来不错,每个上载文件的链接都换行了,等等。但是,很明显,我也不想将普通文本,textarea,checkbox,radio等字段也用作链接。

我已向插件开发人员发送了支持票,但尚未听到任何回音。我可以采取什么方法使它正常工作?

2 个答案:

答案 0 :(得分:1)

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

当我使用dropfiles标记作为必填字段时,我注意到它只是发生了。 [dropfiles * ...]

联系表7具有函数“ replace_tags_callback”,该函数应用了过滤器“ wpcf7_mail_tag_replaced _ {$ type}”。

问题是{$ 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);

因此,类的开始现在是这样的:

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);
    }



}

我已将该错误报告给插件作者,并希望他/她将在下一版本中对其进行更新。

答案 1 :(得分:0)

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

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; 
}