我在表单中使用“Contact Form 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] 字段看起来很棒,每个上传文件的链接都换行等。但我显然不希望普通文本、文本区域、复选框、单选等字段也成为链接。
我已经向插件开发人员发送了支持请求,但还没有收到任何回复。我可以采取什么方法来实现这一目标?
最佳答案
我花了 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);
所以现在开始上课是这样的:
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);
}
}
我向插件作者报告了这个错误,希望他/她能在下一个版本中更新它。
关于php - 联系表 7 - 多文件上传值作为电子邮件中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54824288/