微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gravity Forms 多个多文件上传字段自动添加到 WordPress 媒体库

如何解决Gravity Forms 多个多文件上传字段自动添加到 WordPress 媒体库

我有一个包含多个页面的 Gravity 表单,并且在至少 3 个页面上有一个文件上传字段。我试图从这些字段中的每一个获取上传的项目,并自动将它们添加到媒体库中以供进一步操作。 Using this solution 我能够从第一页获取文件并将它们上传到媒体库,但是,我不知道如何从其他两个页面获取文件

下面是我的代码片段。如果我调整 $submit = $entry["4"]; 多重上传的字段 ID 以包含额外的上传字段,它会破坏我的 foreach() 语句,说 invalid argument is supplied.

我还尝试添加额外的 $submit 部分以包含其他字段 ID,但它只需要最后一个字段 ID 条目。多次添加这整段代码也不起作用,因为它是相同的形式,只是不同的页面,我得到:

致命错误:无法重新声明 copy_post_image2()(先前声明 在 ...\functions.PHP

//Multi-file upload code: upload multiple files through Gravity Forms and have them automatically moved to the wordpress Media Library
add_filter("gform_after_submission_1","my_add_post_attachments",10,2); //Your form ID
function my_add_post_attachments($entry,$form){


$submit = array();
$submit = $entry["4"]; //The field ID of the multi upload
$submit = stripslashes($submit);
$submit = json_decode($submit,true);

foreach ($submit as $key => $value){

require_once(ABSPATH . 'wp-admin/includes/image.PHP');
require_once(ABSPATH . 'wp-admin/includes/file.PHP');
require_once(ABSPATH . 'wp-admin/includes/media.PHP');
$post_id = 200; //The post ID of the page you are adding images
$url = $value;

$file = copy_post_image2($url,$post_id);

if(!$file)
return false;

$name = basename($url);
$name_parts = pathinfo($name);
$name = trim( substr( $name,-(1 + strlen($name_parts['extension'])) ) );

$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = $name;
$content = '';

$attachment = array(
'post_mime_type' => $type,'guid' => $url,'post_parent' => $post_id,'post_title' => $title,'post_content' => $content,);

$id = wp_insert_attachment($attachment,$file,$post_id);
if ( !is_wp_error($id) ) {
wp_update_attachment_Metadata( $id,wp_generate_attachment_Metadata( $id,$file ) );
}
}
}

function copy_post_image2($url,$post_id){
$time = current_time('MysqL');

if ( $post = get_post($post_id) ) {
if ( substr( $post->post_date,4 ) > 0 )
$time = $post->post_date;
}

//making sure there is a valid upload folder
if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
return false;

$name = basename($url);

$filename = wp_unique_filename($uploads['path'],$name);

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";

$uploaddir = wp_upload_dir();
$path = str_replace($uploaddir["baseurl"],$uploaddir["basedir"],$url);

if(!copy($path,$new_file))
return false;

// Set correct file permissions
$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
@ chmod( $new_file,$perms );

// Compute the URL
$url = $uploads['url'] . "/$filename";

if ( is_multisite() )
delete_transient( 'dirsize_cache' );

$type = wp_check_filetype($new_file);
return array("file" => $new_file,"url" => $url,"type" => $type["type"]);

}

关于如何添加额外的多文件上传字段的任何建议将不胜感激。

解决方法

根据错误,您可能两次声明了 copy_post_image2 函数。我没有看到它在你共享的代码中列出了两次,但如果你在你的 functions.php 文件中搜索它,我敢打赌你会找到它的第二个副本。

至于从所有页面上的所有文件上传字段复制所有图像的最终目标,请查看 Gravity Forms Media Library。它会像魔术一样处理这个问题,并支持通过合并标签以不同大小显示图像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。