我在我的表单中使用'联系表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,复选框,无线电等字段也是链接。
我已经向插件开发者发送了支持票,但还没有收到任何回复。我可以采取什么方法来实现这个目标?
我花了很多时间来讨论这个问题,但我想我已经找到了问题。
我注意到它只是在我使用dropfiles标签作为必填字段时发生的。 [dropfiles * ...]
联系表单7有一个函数“replace_tags_callback”,它应用过滤器“wpcf7_mail_tag_replaced _ {$ type}”。
问题是{$ type}来自$ form_tag-> type,在我们的例子中可以是“dropfiles”或“dropfiles *”。如果CF7使用$ form_tag-> basetype获得它,那么两个标签都是一样的。
所以为了解决这个问题,我在第7行的contact-form-7-drop-files / frontend / index.php中为“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);
}
}
我向插件作者报告了这个错误,并希望他/她将在下一个版本中更新它。
我找到了一个临时解决方案 - 直到我从插件开发者那里获得永久解决方案。我更改了代码如下:
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;
}