将重力表单中的图像保存为发布附件可重复文件上载

时间:2013-03-13 作者:Dom

我正在尝试使用Gravity表单和Gravity表单+自定义帖子类型插件将多个图像作为附件上传到帖子。

我有一个使用Gravity Forms gform\\u column\\u input\\u过滤器的可重复文件上载字段。看见this question 这是如何实现的。

然后我改编了这个code from BandonRandon 要使用可重复文件上载字段,请执行以下操作:

 add_filter("gform_after_submission", "nifty_add_post_attachements", 10, 3);
 function nifty_add_post_attachements($entry) {

 //you\'ll need this later, TRUST ME.
 if ( !function_exists(\'wp_generate_attachment_metadata\') ) {
    require_once(ABSPATH . \'wp-admin/includes/image.php\');
 }

//do we even have a file?
 if ( isset($_FILES[\'input_2\']) ) {
     $file_urls = $entry[\'2\']; //great but what is its url?
     $file_array = unserialize($file_urls);

     foreach ( $file_array as $file_url ) {

         $upload_dir = wp_upload_dir(); //where do you want to put it?
         $file_data = file_get_contents($file_url); //show me what you\'re made of
         $filename = basename($file_url); //so cute but what\'s its name?
         if(wp_mkdir_p($upload_dir[\'path\'])) //can we put it there?
         $file = $upload_dir[\'path\'] . \'/\' . $filename; //yes great
         else //or no, okay fine let\'s try somewhere else
         $file = $upload_dir[\'basedir\'] . \'/\' . $filename; //get the whole location
         file_put_contents($file, $file_data); // tada home at last

        $wp_filetype = wp_check_filetype($filename, array(\'jpeg\' => \'image/jpeg\',\'jpg\' => \'image/jpeg\', \'gif\' => \'image/gif\', \'png\' => \'image/png\') ); //is it the right type of of file?
         $attachment = array( //set up the attachment
         \'post_mime_type\' => $wp_filetype[\'type\'],
         \'post_title\' => sanitize_file_name($filename),
         \'post_content\' => \'\',
         \'post_status\' => \'inherit\'
         );

        $attach_id = wp_insert_attachment( $attachment, $file, $entry[\'post_id\'] ); //insert attachment
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); //asign the meta
        wp_update_attachment_metadata( $attach_id, $attach_data ); //update the post
    }
    //remove the entry
     //(to keep the entry comment or delete these line)
     // IMPORTANT See:  http://pastebin.com/quvsvGHJ for the function
     /*if(!function_exists(\'sedc_gform_remove_entries\')){ die(\'next time maybe you should read the comments\');}
     else{ nifty_gform_remove_entries($entry, $form );}*/
 }
}
我尝试在nifty\\u add\\u post\\u attachements函数的顶部使用以下代码转储$\\u文件[\'input\\u 2”“],但屏幕上没有显示输出,帖子仍在创建中,表单ajax加载微调器只是挂起。

<pre>
    <?php print_r($_FILES[\'input_2\']); die();?>
</pre>
如有任何正确方向的帮助或指点,将不胜感激。

1 个回复
SO网友:GhostToast

也许不需要一个可重复的上传字段,而是使用条件逻辑连接下拉列表或收音机,询问用户需要多少次上传(1、2、3、4、5…),然后是一系列预先构建的上传框,根据所选的数字显示。这并不是百分之百的动态,但希望你在一开始就有一些限制。

然后,我将这些存储为元字段并引用它们。您可以将每个上载框映射到元字段。然后,当该帖子生成时(这是一篇帖子,对吗?)您可以引用post\\u meta。

结束